UIFont fontWithName return nil

This code returns Me nil in the font.

I am adding the open sans ttf file to the project folder.

What am I missing?

UIFont *font = [UIFont fontWithName:@"OpenSans-Bold" size:fontSize]; if (font) { [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:font size:fontSize] range:NSMakeRange(location, length)]; label.attributedText = str; } 

enter image description here

+8
objective-c uifont
source share
1 answer

First of all, are your .ttf files registered in your .plist file? Secondly, have your fonts been added to Copy Resources ? (under Target-> Build Phases)

Then try this code to indicate all the fonts used and their names. This way you can make sure that you are using the correct identifier for your font.

 for (NSString* fontFamily in [UIFont familyNames]) { NSArray *fontNames = [UIFont fontNamesForFamilyName:fontFamily]; NSLog (@"%@: %@", fontFamily, fontNames); } 

Edit

Since this answer is a bit old, I updated it with the corresponding answer for Swift 3.0 :

 _ = UIFont.familyNames.map { let fontNames = UIFont.fontNames(forFamilyName: $0) print("Font Family: \($0), Font Names: \(fontNames)\n") } 
+11
source share

All Articles