How to set a custom font for entire iOS app without specifying size
I'm trying to apply a custom font throughout my iOS app
. I found that I could use:
[[UILabel appearance] setFont:[UIFont fontWithName:@"Proxima Nova" size:17.0]];
To set the default font and size for all
UILabels
. However, not all myUILabels
share the same font size.In Set a default font for whole iOS app?, someone had the same concern, and was told to set the size parameter to 0.0 to only set the font and not font size.
- When I tried doing this, all the
UILabel
text in my app disappeared (because evidentlyiOS
took the 0.0 font size literally).
Any suggestions as to how I can universally set a font but not size? Thanks a lot!
Solution 1:
- (void)viewDidLoad
{
[super viewDidLoad];
[self setFontFamily:@"FagoOfficeSans-Regular" forView:self.view andSubViews:YES];
}
-(void)setFontFamily:(NSString*)fontFamily forView:(UIView*)view andSubViews:(BOOL)isSubViews
{
if ([view isKindOfClass:[UILabel class]])
{
UILabel *lbl = (UILabel *)view;
[lbl setFont:[UIFont fontWithName:fontFamily size:[[lbl font] pointSize]]];
}
if (isSubViews)
{
for (UIView *sview in view.subviews)
{
[self setFontFamily:fontFamily forView:sview andSubViews:YES];
}
}
}
Solution 2:
Here is a solution in Objective-C, put this category anywhere you want to change UILabel
Apperance
without setting UILabel
FontSize
:
@implementation UILabel (SubstituteFontName)
- (void)setSubstituteFontName:(NSString *)name UI_APPEARANCE_SELECTOR {
self.font = [UIFont fontWithName:name size:self.font.pointSize];
}
@end
Then, you can change the Apperance
with:
[[UILabel appearance] setSubstituteFontName:@"SourceSansPro-Light"];