40
34

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Swift] UIButtonでテキストを装飾する╭( ・ㅂ・)و ̑̑ グッ !

Last updated at Posted at 2014-12-20

経緯

華やかなボタンを作りたい╭( ・ㅂ・)و ̑̑ グッ !

コード

早速ですが、Playgroundでコーディングをしてみました!

// Playground - noun: a place where people can play

import UIKit

let button = UIButton(frame: CGRectMake(0, 0, 100, 100))


// NG: button.titleLabel!.text = "like"
button.setTitle("like", forState: UIControlState.Normal)


// NG: button.titleLabel!.textColor = UIColor.orangeColor()
button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)

button.titleLabel!.font = UIFont(name: "Helvetica-Bold",size: CGFloat(30))

button.layer.masksToBounds = true
button.layer.cornerRadius = 50.0
button.backgroundColor = UIColor.blueColor()

※ コードからは、╭( ・ㅂ・)و ̑̑ グッ ! の絵文字を外しています。

成果物

like.png

うはwwwwwwwださっwwwwwww

まとめ

「色も変わらんし、フォントも変わらんし(゚Д゚)ハァ?」

と小一時間嘆いている、わたしにそっくりなそこのあなた!

これは使うな

button.titleLabel!.text = "like"
button.titleLabel!.textColor = UIColor.orangeColor()
button.titleLabel!.shadowColor = UIColor.redColor()

これを使え

button.setTitle("like", forState: UIControlState.Normal)
button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
button.setTitleShadowColor(UIColor.redColor(), forState: UIControlState.Normal)

公式ドキュメントに使うなって書いてあるやん…。

Although this property is read-only, its own properties are read/write. Use these properties primarily to configure the text of the button. For example:

Do not use the label object to set the text color or the shadow color. Instead, use the setTitleColor:forState: and setTitleShadowColor:forState: methods of this class to make those changes.

The titleLabel property returns a value even if the button has not been displayed yet. The value of the property is nil for system buttons.

setTitle についても書いて欲しかったな_| ̄|○
setTitleShadowColor こっちも使うなってさ!

記述的に、StoryBoardとかで配置したボタンを後から変更する分には大丈夫そう?
分かり次第、追記しておきます╭( ・ㅂ・)و ̑̑ グッ !

40
34
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
40
34

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?