'Setting the titleLabel.font property of a of a UIButton not working

In currently working with iOS 7 and I an attempting to increase the font size of the titleLabel of my UIButton. I am doing it like this,

[[_centerButton titleLabel] setFont:[UIFont boldSystemFontOfSize:28.0]];

However this does not do anything. Every time I compile with a different size the font always stays the same. Even when I completely delete that line the font stays the same. Apparently it is sticking to the default size.

How can I increase the font size of my UIButton title?



Solution 1:[1]

Using Xcode 13, I had to change Style property from Plain to Default on Interface Builder.

enter image description here

Then after set the title, I finally could change the font programmatically without a problem:

button.setTitle("The title", for: .normal)
button.titleLabel?.font = UIFont(name: "Montserrat-SemiBold", size: 15)

Solution 2:[2]

Have a look at the official UIButton class reference on https://developer.apple.com/library/ios/documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html#//apple_ref/occ/instp/UIButton/titleLabel

It says that you're actually able to set the font with your method:

button.titleLabel.font = [UIFont systemFontOfSize: 12];

Solution 3:[3]

Well I had the same problem, have you used interface builder? after I set the title property to Plain, it's working, very very weird.

Solution 4:[4]

Are you using Storyboards? If so, when you click on a UIButton you can see in the attributes inspector an attribute called Font. By default, it appears as "Custom" (it appears blank). Click on the T symbol and choose System. Then you can choose the font size.

Click on the T symbol

Choose system, now you can set a size

Hope it helps.

Solution 5:[5]

A little bit late...some properties of titleLabel are reset after setTitle().link

Solution 6:[6]

If you are trying to do this with the new UIButton.configuration in iOS 15, changing the titlelabel?.font won't work because of the configuration field will override it with a system style since you now need to set configuration?.title = "some title".

You need to create an AttributedString and set configuration?.attributedTitle = yourAttributedString

My whole app is programmatic so here is an example of a custom button "red pill" style button I created that dynamically sets the button title with a convenience init.

class APButton: UIButton {

    override init(frame: CGRect) {
        super.init(frame: frame)
        configure()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    convenience init(title: String) {
        self.init(frame: .zero)
        setDynamicTitle(title: title)
    }
    
    private func configure() {
        configuration = .filled()
        configuration?.cornerStyle = .capsule
        configuration?.baseBackgroundColor = .red
        configuration?.baseForegroundColor = .white

        translatesAutoresizingMaskIntoConstraints = false
    }
    
    private func setDynamicTitle(title: String) {
        let font = UIFont.systemFont(ofSize: 20)
        let container = AttributeContainer([NSAttributedString.Key.font: font])
        let attribString = AttributedString(title, attributes: container)
        
        configuration?.attributedTitle = attribString
    }

}

Solution 7:[7]

In your case it's the problem you don't set the UIControlStateNormal which should get the specific fontSize.

I normally create a class of type UIButton and set the fontSize inside awakeFromNib. Look at this post.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 pableiros
Solution 2 Niklas
Solution 3 harthoo
Solution 4 brandonscript
Solution 5 Community
Solution 6 Andrew Prokop
Solution 7 Community