Reputation: 269
How would you add a custom font in Xcode 5 and how would you change every label in the project to that font? Because I've heard you can only do this programmatically?
Upvotes: 23
Views: 35137
Reputation: 36
Much better option is to add the new font to your system/OS fonts directory and then from there it can be added in the storyboard or used with NSAttributed strings.
Steps:
Hope that helps!
Upvotes: 2
Reputation: 36447
You can create custom font in Xcode using following steps :
Now, you can use font in label as :
[lblName setFont:[UIFont fontWithName:@“CustomFontName” size:14.0]];
Upvotes: 1
Reputation: 3953
The answers here are fine, but I'd recommend setting as little as possible in your custom label and definitely not hardcoding the size. You can easily do it like this:
#import "CustomFontLabel.h"
@implementation CustomFontLabel
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setFontStyle];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self setFontStyle];
}
return self;
}
- (void)setFontStyle {
CGFloat fontSize = self.font.pointSize;
[self setFont:[UIFont fontWithName:@"LindenHill" size:fontSize]];
}
@end
This will respect all of the other settings you set in your Storyboard making it way easier to manage everything.
Upvotes: 1
Reputation: 8247
You need to set every label programmatically with your custom Font.
To use custom font :
1/ add your custom font in your project like resources (font .ttf or .otf)
2/ in your info.plist add key UIAppFonts (Fonts provided by application) and and the name of each custom font (for example : SohoGothicStd.ttf)
3/ you can create macro for use your font
#define FONT_SOHO_STD(s) [UIFont fontWithName:@"SohoGothicStd" size:s]
4/ use this macro for a label par exemple :
_myLabel.font = FONT_SOHO_STD(15.0f);
Upvotes: 70
Reputation: 10615
I believe you need to use [UILabel appearance]
proxy to set custom font for all labels across your application. Add following lines to your AppDelegate
didFinishLaunchingWithOptions
function to set custom font for all UILabel
s in your project.
UIFont *newFont = [UIFont fontWithName:@"My-Custom-Font-Name" size:14];
[[UILabel appearance] setFont:newFont];
NOTE: You need to make sure your fonts are in your Xcode project.
Here are the steps you can follow to add custom font to the project.
Xcode
as a
resourceInfo.plist
file called UIAppFonts
.UIAppFonts
arrayInfo.plist
Steps taken from: Can I embed a custom font in an iPhone application?
Upvotes: 15
Reputation: 2600
Yes you need to do it through code. I dont think XCode5 has added any new feature like such. To do it programmatically do the following :
Make a class which is subclass of UILabel then say you have something like Title which has font size 14 with Helverica-Bold
IN .h file
#import <UIKit/UIKit.h>
@interface MyLabel : UILabel {
}
@end
In .m file
#import "MyLabel.h"
@implementation MyLabel
- (id)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder];
if (self) {
self.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
}
return self;
}
@end
Another example without using separate sub class
-(UILabel *) myTitleLabel {
UILabel *label = [[UILabel alloc] init];
label.backgroundColor=[UIColor clearColor];
[label setFont:[UIFont fontWithName:@"Helvetica-Bold" size:14]];
label.textColor=[UIColor colorWithRed:(79.0 / 255.0) green:(79.0 / 255.0) blue:(79.0 / 255.0) alpha: 1];
[label setTextAlignment:NSTextAlignmentLeft];
[label sizeToFit];
return label;
}
Then you can use myTitleLabel class to create a object so that all the titleLabel Object will have same color and font size.
Upvotes: 5