I've been looking for close to 2 hours and could not find a solid answer for this question.
I tried to use appearance mechanism but it's seems you can't change the font with it.
I've looked at the answer suggested here : How to change all UIButton fonts to custom font
There are 3 answer :
Edit : with answer 3 if you implement the - (id)initWithCoder:(NSCoder *)aDecoder method of uibutton this enables you to simply change the class in interface builder, but you still need to do it for all the button in the project.
Does anyone have any new idea?
To set a different font on UIButton programmatically you will need to create a new instance of UIFont object. The initializer of UIFont accepts two arguments: name and size. where the “GillSans-Italic” is a font name you want to set.
To change the font or the size of a UILabel in a Storyboard or . XIB file, open it in the interface builder. Select the label and then open up the Attribute Inspector (CMD + Option + 5). Select the button on the font box and then you can change your text size or font.
You cannot stop the user from changing the system font size; you'll just have to use dp and ignore the lint errors!
In Swift3
UIButton.appearance().titleLabel?.font = font
For some reason that only the gods from Apple know, the
[[[UIButton appearance] titleLabel] setFont:fontButton];
doesn't work. A solution that worked for me was found in swift in
How to set UIButton font via appearance proxy in iOS 8?
The solution in Objective-C is to create a category that exposes the titleLabel font like this
UIButton+TitleLabelFont.h
#import <UIKit/UIKit.h>
@interface UIButton (TitleLabelFont)
@property (strong, nonatomic) UIFont *titleFont;
@end
UIButton+TitleLabelFont.m
#import "UIButton+TitleLabelFont.h"
@implementation UIButton (TitleLabelFont)
- (UIFont *)titleFont {
return self.titleLabel.font;
}
- (void)setTitleFont:(UIFont *)titleFont {
self.titleLabel.font = titleFont;
}
@end
Then import the category UIButton+TitleLabelFont in the source that you're setting the appearance simply with
#import "UIButton+ASLAdditions.h"
and you'll be able to set the appearance with
[UIButton appearance].titleFont = fontButton;
Tested and worked perfectly for me
You can use UIAppearance to customize view of any control. More information you can check out in this very helpful article on NSHipster.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With