I want to keep custom fonts in iOS app. Currently, I\'m using iOS default fonts. Is there any possible way for the same?
How do I include the custom font so that I c
Copy your font file into resources
Add a key to your Info.plist file called UIAppFonts
. ("Fonts provided
by application")
Make this key an array
For each font you have, enter the full name of your font file
(including the extension) as items to the UIAppFonts
array
Save Info.plist
Now in your application you can simply call following method to get the custom font to use with your UILabel
and UITextView
, etc…
[UIFont fontWithName:@"CustomFontName" size:15];
func allFonts(){
for family in UIFont.familyNames(){
println(family)
for name in UIFont.fontNamesForFamilyName(family.description)
{
println(" \(name)")
}
}
}
UILabel
:Add code in AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
[[UILabel appearance] setFont:[UIFont fontWithName:@"YourFontName" size:17]];
...
}
viewConroller
same as font size set in view design:- (void)viewDidLoad
{
[super viewDidLoad];
[self setFontFamily:@"YourFontName" 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];
}
}
}
Ok, from all the other answers you are now very well familier that how to add a custom font (other than iOS default font) into app.
So now what? One thing is still remaining, I'm not sure if you want to use the same size custom font everywhere. If so,
I'm using this way to have custom font in my app.
If your app will have a single custom font then you can use it like this way for entire app.
#define appFontBold(x) [UIFont fontWithName:@"CustomFont-Bold" size:x]
#define appFontLight(x) [UIFont fontWithName:@"CustonFont-Light" size:x]
#define appFontRegular(x) [UIFont fontWithName:@"CustonFont-Regular" size:x]
I've added these macros globally (so can access it from anywhere)
and you can use it like this, yourLabel.font = appFontRegular(12);
or yourTextField.font = appFontBold(14);
this helps you in following situations,
[UIFont fontWithName: size:]
; everywhere.If your app will have more than one custom font then you can use it like this way for entire app.
then also you can define different macros like this,
Then above macro will be the same (as this is the maximum used fonts in app), for other fonts :
#define appFontBoldOtherFont(x) [UIFont fontWithName:@"CustomOtherFont-Bold" size:x]
#define appFontLightOtherFont(x) [UIFont fontWithName:@"CustonOtherFont-Light" size:x]
#define appFontRegularOtherFont(x) [UIFont fontWithName:@"CustonOtherFont-Regular" size:x]
Add your custom font into your project , i.e. Dragged the font file(CALIBRIZ_0.TTF) into XCode project.
Edit Info.plist
: Add a new entry with the key "Fonts provided by application
".
For each of your files, add the file name to this array
Now set font to your label
yourLabel.font = [UIFont fontWithName:@"Calibri" size:15];
Make a category:
#import "UILabel+Helper.h"
@implementation UILabel (Helper)
- (void)setSubstituteFontName:(NSString *)name UI_APPEARANCE_SELECTOR {
self.font = [UIFont fontWithName:name size:self.font.pointSize]; }
@end
Then in AppDelegate.m:
[[UILabel appearance] setSubstituteFontName:@"SourceSansPro-Regular"];
This will change font in whole app even on UIButton
, UILabel
, inside UITableViewCell
, UICollectionViewCell
, UIView
, UIContainerView
, or anywhere in application without changing the font point size.
This is memory efficient approach rather than enumerating all views in your UIViewController
.
1- Drag & Drop Font file .ttf to your project
2- Verify the font is in the project.
By selecting the font, and verifying “Target Membership” in the Utilities area.
3- Add info in plist
4- In delegate add this code
[[UILabel appearance] setFont:[UIFont fontWithName:@"YourFontName" size:11.0]];
[[UITextField appearance] setFont:[UIFont fontWithName:@"YourFontName" size:11.0]];
[[UITextView appearance] setFont:[UIFont fontWithName:@"YourFontName" size:11.0]];
I hope this will help
If you want to change all fonts for all UILabel in one place, you need to create a category of UILabel.
for example:
#import <UIKit/UIKit.h>
@interface UILabel (AppFont)
@end
@implementation UILabel (AppFont)
-(void)awakeFromNib
{
self.font = [UIFont fontWithName:@"yourCustomFont" size:self.font.pointSize];
}
@end
after you've created the category, you will need to import this class for all the file, or you can add it to '.pch' file for your project.
same solution for buttons / textview ..