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


経緯

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

コード

早速ですが、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()

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

成果物

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