23

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?

pableiros
  • 14,932
  • 12
  • 99
  • 105
gossainapps
  • 723
  • 1
  • 7
  • 18

7 Answers7

68

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)
pableiros
  • 14,932
  • 12
  • 99
  • 105
  • 1
    Thanks! This was my case and for those who have a design system or something alike and dont wanna customize storyboards you can `button.configuration = nil`. For more info about this iOS 15 changes pls check [here](https://useyourloaf.com/blog/button-configuration-in-ios-15/) – rgkobashi Nov 15 '21 at 05:17
  • 1
    Yeah, this really saved me. Thank you. Not sure why Apple felt the need to tinker with things here. – Brian Sachetta Nov 20 '21 at 01:24
  • So how exactly style works? if we have the style set to plain. what is the font used? and its weight! can someone explain? – hasan Dec 18 '21 at 22:30
  • This works for me, though I'm not sure what happened. – chengsam Dec 29 '21 at 07:20
14

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];
  • 1
    The author seems to be aware that the method is right, they obviously wonder why it doesn't work in their case. If you don't have enough info to answer the question, you can ask clarifying questions. Stating "it should work" while it doesn't is not an answer. – lazarevzubov Oct 19 '21 at 06:37
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.

harthoo
  • 813
  • 10
  • 11
2

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.

brandonscript
  • 68,675
  • 32
  • 163
  • 220
Axort
  • 2,034
  • 2
  • 23
  • 24
1

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

Community
  • 1
  • 1
TomCobo
  • 2,886
  • 3
  • 25
  • 43
0

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
    }

}
-2

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.

Community
  • 1
  • 1
Alex Cio
  • 6,014
  • 5
  • 44
  • 74