Search code examples
iosfontsnsstringnsattributedstringnsmutablestring

Set bold font to NSString iOS


I have imported 2 font files to my resources group in my project, and also added them to my plist file:

pt-sans.narrow-bold.ttf and PT_Sans-Narrow-Web-Bold.ttf

I want to set bold font to attributed string, but it can't find my fonts, i am getting this error:

(UIFont*) nil

This is how i am setting the font:

UIFont *font_bold=[UIFont fontWithName:@"PT Sans Narrow Bold" size:14.0f];

It needs to be font from this family. Any idea, what am i doing wrong?


Solution

  • You can check the font's are accessible by adding this to your didFinishLaunchingWithOptions

        NSArray *fontFamilies = [UIFont familyNames];
    
    for (int i = 0; i < [fontFamilies count]; i++)
    {
        NSString *fontFamily = [fontFamilies objectAtIndex:i];
        NSArray *fontNames = [UIFont fontNamesForFamilyName:[fontFamilies objectAtIndex:i]];
        NSLog (@"%@: %@", fontFamily, fontNames);
    }
    

    and as @mikle94 said, caps is probably your issue.