I need to set the font size of the title UILabel of a UIButton programmatically.
I hope it will be help to you
[_button.titleLabel setFont:[UIFont systemFontOfSize:15]];
good luck
This way you can set the fontSize and can handle it in just one class.
extension of UIButton and added following code:- (void)awakeFromNib{
[super awakeFromNib];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.titleLabel setFont:[UIFont fontWithName:@"font"
size:self.titleLabel.font.pointSize]];
[self setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
}
UIButton inside CodeNow if you create a UIButton inside your code, #import the extension of yourUIButton` and create the Button.
Interface BuilderIf you create the UIButton inside the Interface Builder, select the UIButton, go to the Identity Inspector and add the created extension as class for the UIButton.
Check on custom font name whether checkbox on "Target membership" is added. This should help.
Swift 3:
myButton.titleLabel?.font = myButton.titleLabel?.font.withSize(40)
you can also customise button font with bold, italic. this example with bold system font size.
[LoginButton.titleLabel setFont:[UIFont boldSystemFontOfSize:15.0f*Ratio]];
swift 4.x
button.titleLabel?.font = UIFont.systemFont(ofSize: 20)