Change default font to custom fonts in whole app - ios

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 can use it?

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.

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];
Check which fonts are available in your resources :
func allFonts(){
for family in UIFont.familyNames(){
println(family)
for name in UIFont.fontNamesForFamilyName(family.description)
{
println(" \(name)")
}
}
}
Change font for whole application in UILabel :
Add code in AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
[[UILabel appearance] setFont:[UIFont fontWithName:#"YourFontName" size:17]];
...
}
Change font in one 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];
}
}
}

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

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,
Whenever you needs to change the custom font, you only need to change font name in macros.
Your code will looks clean, no need write [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];

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 ..

Related

Avoid Bold effect ios Settings

I have more than 150 xibs in my app, I don't want to update the font for each xib, When I updates the font from settings to BOLD, it disturbs the font size of my app, I want to avoid that.
How can I avoid this?
Thanks in advance.
Hi You could Programmatically set the font as bold. Try This-
In Your View Controller. m -->
- (void)viewDidLoad
{
[super viewDidLoad];
[self makeFontLabel:self.label];
}
- (void)makeFontBold:(UILabel *)label
{
UIFont *labelFont = label.font;
UIFont *boldFont = [UIFont fontWithName:[NSString stringWithFormat:#"%#-Bold",labelFont.fontName] labelFont.pointSize];
label.font = boldFont;
}
Hope it helps you!

How do I set bold font on UILabel in iOS?

I want to set bold font on UILabel. I generate label by coding. when i try to set bold font on label ios set system regular font but i want to set 'Noto sans-Bold' font. In storyboard set font completely.
My code is
UILabel *lblName=[[UILabel alloc]initWithFrame:CGRectMake(0, 25, 100, 21)];
lblName.text=#"Hello World";
[lblName setFont:[UIFont fontWithName:#"Noto Sans-Bold" size:15.0]];
[self.view addSubview:lblName];
If you haven't got the formatting of your font title correct, it will automatically default back to system font.
The issue is with your font name and since Noto isn't native it's going to be difficult to know exactly what name you need to provide.
Make sure you have added that font to your project.
You can check your available fonts by running this code:
SWIFT
for familyName in UIFont.familyNames() {
print("\n-- \(familyName) \n")
for fontName in UIFont.fontNamesForFamilyName(familyName) {
print(fontName)
}
}
OBJECTIVE-C
for (NSString *familyName in [UIFont familyNames]){
NSLog(#"Family name: %#", familyName);
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
NSLog(#"--Font name: %#", fontName);
}
}
lblname.font = [UIFont fontWithName:#"Noto Sans-Bold" size:18];
and one more thing check your font correctly it is available in list or not.
Here is a list of available fonts .
List of available fonts
EDIT:-
or if you want to set font which not available in xcode then you have to first install it in XCode
please follow this tutorial.
Install custom fonts
As said by Jilouc follow this procedure.
Add the font files to your resource files
Edit your 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
On the example below, I've added the font "DejaVu Sans Mono":
In your application you can
the use [UIFont fontWithName:#"DejaVuSansMono-Bold" size:14.f].
Or if you want to use it in html/css, just use font-family:DejaVu
Sans Mono;

Unable to set custom font for the UILabel in XCode

I am unable to set custom font for the UILabel in XCode.
This is what I've tried:
Download "JennaSue" font -- http://www.dafont.com/jenna-sue.font
Open "app-info.plist" under "Supporting Files" folder
Add new row in the list with key "Fonts provided by application"
In this array add "JennaSue.ttf"
Use it in the code like this:
self.someLabel.font = [UIFont fontWithName:#"JennaSue" size:14];
And nothing happens -- the default font is visible.
Why? What am I doing wrong? How can I fix it?
Be sure your font is in Bundle Resources. For some reason Xcode it is not importing custom font properly most of the time:
I've got the font working:
Example code: here
Go to your project's info.plist file,
Right click and select Add row,
Type Fonts provided by application in the new row,
Type in the desired font name as items for this key.
Drag and drop the font file into the project,
Apply it to the text you want:
someLabel.font = [UIFont fontWithName:#"JennaSue" size: 12.0];
I think you've missed step 5 up there.
Update: When doing step 5, remember to check the marks for copying the actual file into project directory:
Remember to clean your project by pressing "command+alt+shift+K" (IRRC!)
And then go to your
and then go to your project's Build Phases, and make sure you can see your .ttf file file among the resource files:
P.S. I'm not on my Mac at the moment, so I used screenshots from this tutorial. That's why I grayed out the unnecessary lines for your issue.
Check this code:
NSArray *names = [UIFont familyNames];
NSLog(#"Font FamilyNames : ");
for (NSString *name in names) {
NSLog(#"Font Family: %#",name);
NSArray *fontFaces = [UIFont fontNamesForFamilyName:name];
for (NSString *fname in fontFaces) {
NSLog(#" %#",fname);
}
}
self.someLabel.font = [UIFont fontWithName:#"use correct name" size:self.someLabel.font.pointSize];
and use the same name printed with NSLog.
After adding custom font to Xcode don't forget to add font to plist.
Project settings > Info > Custom IOS target properties
Add property Fonts provided by application and type your custom font(filename of font file).
Then you can use that code example
self.someLabel.font = [UIFont fontWithName:#"JennaSue" size:14];
Notice: make sure that you use right font name in code. To do that you should add this font to your Mac's font book, open font book, choose that font and check the right name font.
Hope this helps
self.someLabel.font = [UIFont fontWithName:#"JennaSue.ttf" size:self.someLabel.font.pointSize];
i used following code and it's done
UILabel * lblTest = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 200, 100)];
lblTest.font = [UIFont fontWithName:#"JennaSue" size:25.0];
lblTest.text = #"Hello World";
[self.view addSubview:lblTest];
and if still not working looking at your system font book and search that font and That Font you have to use to get it work
also try this lblTest.font = [UIFont fontWithName:#"Jenna Sue" size:25.0];
The font could be corrupted or something. I tried the solutions provided by Gabriel.Massana & Neeku and it worked on some fonts. Try adding the font to your Mac. If it showed verification problem, most probably it's not gonna work in your app. Hope this helps
is the file visible in Compile Source now…if yes then you may have the font name wrong..sometimes the font names are different then their file name…try running a
for (NSString *familyName in [UIFont familyNames])
{
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName])
{
NSLog(#"%#", fontName);
}
}
this lines of code..you will be able to see the actual name of the font and use that name instead
Here is #Ritu's answer in Swift 2. Just put this in your AppDelegate, or in viewDidLoad method of your View Controller:
let names = UIFont.familyNames()
for name in names {
print("Font Family: \(name)")
let fontFaces = UIFont.fontNamesForFamilyName(name)
for fname in fontFaces {
print(" \(fname)")
}
}
You will have all your font names in console log, just copy and paste the one you need in your code.

Wont change font to customFont

I've looked through almost every thread on creating a custom font for a Label. What am i doing wrong?
plist:
bundle resources:
resources:
The code:
UILabel *headLine = [[UILabel alloc] initWithFrame:CGRectMake(screenWidth/2-55, screenHeight/2-100, 110, 35)];
headLine.textColor = headlineColor;
headLine.text = #"HEADLINE";
UIFont *pacificoFont = [UIFont fontWithName:#"Pacifico" size:35.0f];
headLine.textAlignment = UITextAlignmentCenter;
[headLine setFont:pacificoFont];
[self.view addSubview: headLine];
There are a lot of errors that can be done with custom fonts (more info here).
However, I suspect that your family name is wrong. Use this to print the available family names and check if "Pacifico" is there:
for (NSString* family in [UIFont familyNames]) {
NSLog(#"%#", family);
for (NSString* name in [UIFont fontNamesForFamilyName: family]) {
NSLog(#" %#", name);
}
}
When you declare a UIFont with a custom name you have to specify the family of the font, and not the filename of the ttf file :)
follow the below steps:
1)Add your custom font files into your project using Xcode as a resource
2)Add a key to your Info.plist file called UIAppFonts.
3)Make this key an array
4)For each font you have, enter the full name of your font file (including the extension) as items to the UIAppFonts array
5)Save Info.plist
6)Now in your application you can simply call [UIFont fontWithName:#"CustomFontName" size:12] to get the custom font to use with your UILabels and UITextViews, etc…
note: Make sure the fonts are in your Copy Bundle Resources.

Italic Font Is Not Working on XCode

I have copy my ttf font (ABeeZee-Italic.ttf) from Google WebFonts into Supporting Files and added into plist :
and assign my Label with tag = 4 :
because I have this code to embed fonts into UILabel:
for (UILabel *customLabel in [[self view] subviews]) {
if (customLabel.tag == 1) {
[customLabel setFont:[UIFont fontWithName:#"Raleway-ExtraLight" size:21]];
} else if (customLabel.tag==2){
[customLabel setFont:[UIFont fontWithName:#"ABeeZee-Regular" size:14]];
} else if (customLabel.tag==3){
[customLabel setFont:[UIFont fontWithName:#"ABeeZee-Regular" size:11]];
} else if (customLabel.tag==4){
[customLabel setFont:[UIFont fontWithName:#"ABeeZee-Italic" size:12]];
}
}
it's all working like a charm, but the italic font is not loaded into my iPod (iOS 6.1.3). UILabel with tag 4 is the iOS system font. not ABeeZee-Italic.
I tried to change the tag number, but also failed. Is there any specific requirements for non-regular font to be embedded into iOS?
thank you
UPDATE #iPatel answer : here's how my FontBook looks like, it has same name...
Check what is real name of font (ABeeZee-Italic) in "FontBook" ? and write name of font which is in "FontBook".
And use such like
[myLabelName setFont: [UIFont fontWithName:#"Font Name From Font Book" size:32]];
More information about how to add custom font in app. then read This Answer.
EDITED:
Also try with
Select Your font from your project
right click
Open With External Editor
Install Font
select inserted font
And check what is "Full name", copy and past it in your code:
May be helpful :)
You can do this in two ways.
Using ttf file
[customLabel setFont:[UIFont fontWithName:#"Raleway-ExtraLight.ttf" size:21]];
or
Using font name
[customLabel setFont:[UIFont fontWithName:<Name of Raleway-ExtraLight> size:21]];
In your case just add .ttf in setFont: else get Name of font as shown in below image.
Just right click on .ttf file > Get Info > Full Name (your font name. You can use this name as font name in setFont)
EDIT
Check the full name here and paste it in your code.
[customLabel setFont:[UIFont fontWithName:#"ABeeZee Italic" size:21]];
EDIT 1
May be you need to Register Fonts. Just try to register and check.

Resources