UILabel - set custom fonts - ios

I want to add GillSans-Bold font to a UILabel.
I have set it in the xib file , and I'm also setting it up in my class as follows :
[label setFont:[UIFont fontWithName:#"GillSans-Bold" size:18]];
But , it doesn't seem to work for me.
Any suggestions ?

iPhone 4.3 doesn't have Gill Sans, but iPad has it since 3.2.1.
See this list comparing fonts for iPad 4.3 and iPhone 4.3. To be sure, this is how you get the list of fonts available on your device:
for (NSString *familyName in [UIFont familyNames]) {
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
NSLog(#"%#", fontName);
}
}
If it says
GillSans
GillSans-Bold
GillSans-BoldItalic
GillSans-Italic
then [UIFont fontWithName:#"GillSans-Bold" size:18] should return a valid font.

For this to be working I had to add this font in my project directory , and added this font in the Info.Plist file

Does the font GillSans-Bold exist? Check if [UIFont fontWithName:#"GillSans-Bold" size:18] returns an UIFont, not null.

Swift
let label = UILabel()
label.font = UIFont(name: "Roboto-Regular", size: 16)
label.text = "Your text here"
P.S. Before that you must:
Add custom font in project
Add font names in Info.plist file
Add font files in Copy Bundle Resources
After that you can also use this fonts in storyboards

Related

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;

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.

How to set font name of UILabel as HelveticaNeue Thin in iOS?

I am creating UILabel, for the label i can set the font name as HelveticaNeue Regular, Light, UltraLight etc, But i unable to set the font name as HelveticaNeue Thin, it is not working as expected. I did like,
label.font = [UIFont fontWithName:#"HelveticaNeue-Thin" size:16];
Also i have searched on Google didnt got any solution. How to fix this issue? Thanks.
This font is bundled with iOS 7, so if you're targeting iOS 7 and above your
label.font = [UIFont fontWithName:#"HelveticaNeue-Thin" size:16.0f];
will work.
However if you are targeting iOS 6.1 and below you'll need to embed the font
Updated answer to support swift
let font = UIFont(name: "HelveticaNeue-Thin", size: 16.0)!
UIFontDescriptor *helveticaNeueFamily =
[UIFontDescriptor fontDescriptorWithFontAttributes:
#{ UIFontDescriptorFamilyAttribute: #"Helvetica Neue" }];
NSArray *matches =
[helveticaNeueFamily matchingFontDescriptorsWithMandatoryKeys: nil];
The matchingFontDescriptorsWithMandatoryKeys: method as shown returns an array of font descriptors for all the Helvetica Neue fonts on the system, such as HelveticaNeue, HelveticaNeue-Medium, HelveticaNeue-Light, HelveticaNeue-Thin, and so on.
You can modify the fonts returned by preferredFontForTextStyle: by applying symbolic traits, such as bold, italic, expanded, and condensed. You can use font descriptors to modify particular traits, as shown in Listing 9-2.
referenced by apple
or
this font you can't apply directly.
so you can customize your font
How to use custom fonts in iPhone SDK?
This has worked for me. Write this code below your label declarations.
It sets all the UILabel under a function with same font.
[[UILabel appearance]setFont:[UIFont fontWithName:#"HelveticaNeue-Thin" size:32.0f]];
To set font to particular UILabel use this code :
[labelName setFont:[UIFont fontWithName:#"HelveticaNeue-Thin" size:15.0f]];

Installing custom fonts in Xcode 4.6.3

I'm having an issue installing a custom font (from Google Fonts) in Xcode 4.6.3. I am doing the following:
Downloading my font from Google Fonts
Installing it on my mac so that I can get its system name
drag my font file (Inconsolata.ttf) into the "Supporting Files" folder in Xcode
Go to my Info.Plist file and add Inconsolata.tff as an item in the UIAppFonts array.
try to print out my installed fonts in the list of fonts using
try to use my custom font with it's full name, which I found in Finder
step 5: print out All UIFonts:
// When I search for "Inconsolata" in my Debug Console, it returns nothing
// Therefore I think that the font simply isn't being installed...
for (NSString *familyName in [UIFont familyNames]) {
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
NSLog(#"%#", fontName);
}
}
step 6: use custom UIFont:
// this line doesn't work.
cell.textLabel.font = [UIFont fontWithName:#"Inconsolata" size:15];
// this line works. it changes font to Helvetica-Bold
// cell.textLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:15];
Fixed by adding target when adding font file, like so

Interface builder is defaulting to Helvetica just now

I have been using a specific font which I've been testing on the iPad directly and it was working fine.
Now all of a sudden, the label or textview using the font are default back to Helvetica.
As a hack I'm setting the font manually, but this is ridiculous
self.taskNameView.font = [UIFont fontWithName:#"BradleyHandITCTT-Bold" size:24.0];
Any idea why all of a sudden, interface builder is freaking out?
I've tried cleaning but that didn't help.
I'm on XCode 4 and working on iOS 4.3
If you just want to override all fonts in your application, maybe a category on UIFont will do the trick?
Try adding this to e.g. your AppDelegate.m:
#implementation UIFont (FontOverride)
+(UIFont*) systemFontOfSize:(CGFloat)fontSize
{
return [UIFont fontWithName:#"radleyHandITCTT-Normal" size:fontSize];
}
+(UIFont*) boldSystemFontOfSize:(CGFloat)fontSize
{
return [UIFont fontWithName:#"radleyHandITCTT-Bold" size:fontSize];
}
+(UIFont*) italicSystemFontOfSize:(CGFloat)fontSize
{
return [UIFont fontWithName:#"radleyHandITCTT-Italic" size:fontSize];
}
#end

Resources