How to Change Font of Uibutton with Swift

How to change font of UIButton with Swift

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.

UIButton font size isn't changing

In Xcode 13 ,UIButton has four type are Plain,Grain,Tinted,Filled .When you create a button in storyboard , button type automatically set with Plain that means new UIButton configurations is on. If you want to old behaviour , you must set style plain to default.

Or , If you want one of the style above . You need to set font like

button.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { incoming in
var outgoing = incoming
outgoing.font = UIFont.systemFont(ofSize: 50)
return outgoing
}

How to change UIButton titlelabel fonts style and size in appearance()?

Ok, try this instead:

Go to the AppDelegate and paste this-

    extension UIButton {
@objc dynamic var titleLabelFont: UIFont! {
get { return self.titleLabel?.font }
set { self.titleLabel?.font = newValue }
}
}

class Theme {

static func apply() {
applyToUIButton()
// ...
}

// It can either theme a specific UIButton instance, or defaults to the appearance proxy (prototype object) by default
static func applyToUIButton(a: UIButton = UIButton.appearance()) {
a.titleLabelFont = UIFont(name: "Courier", size:20.0)

// other UIButton customizations
}
}

And then add "Theme.apply()" here (still in the AppDelegate):

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
//UIApplication.shared.statusBarStyle = .lightContent

Theme.apply() // ADD THIS
return true
}

I did it with Courier font and it worked for me. I think you need to make sure your font is installed in your app.

This is where I got it from: How to set UIButton font via appearance proxy in iOS 8?

Set font for UIButton with extension

You might be using the wrong font name. Add this code to your AppDelegate and check if you're using the right name:

for fontFamilyName in UIFont.familyNames {
print("family: \(fontFamilyName)\n")

for fontName in UIFont.fontNames(forFamilyName: fontFamilyName) {
print("font: \(fontName)")
}
}

EDIT:

This is even cooler to print out the fonts:

dump(UIFont.familyNames)

Also, double check if you have imported your font files and added them to your plist. The font names on your plist must be the same as they will be printed on the code above.



Related Topics



Leave a reply



Submit