How to change font of UIButton with Swift

Stephen Fox picture Stephen Fox · Jul 28, 2014 · Viewed 185.4k times · Source

I am trying to change the font of a UIButton using Swift...

myButton.font = UIFont(name: "...", 10)

However .font is deprecated and I'm not sure how to change the font otherwise.

Any suggestions?

Answer

codester picture codester · Jul 28, 2014

Use titleLabel instead. The font property is deprecated in iOS 3.0. It also does not work in Objective-C. titleLabel is label used for showing title on UIButton.

myButton.titleLabel?.font =  UIFont(name: YourfontName, size: 20)

However, while setting title text you should only use setTitle:forControlState:. Do not use titleLabel to set any text for title directly.