Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
483 views
in Technique[技术] by (71.8m points)

ios - How do I get the font name from an otf or ttf file?

I have used a custom font in my previous app.

The file name was "ProximaNova-Regular.otf" and to load the font I just used...

[UIFont fontWithName:@"ProximaNova-Regular" size:20];

This worked perfectly.

Now in this new app I have three font files...

Dude_Willie.otf
Impact
handsean.ttf

But I'm not sure how to load these.

I have tried

[UIFont fontWithName:<the file name> size:20];

But this just falls back to using Helvetica.

How can I find what name to use?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Right click on the TTF -> Get Info

"Full Name" is what you're looking for.

That's what worked for me with TTFs.

Edit:

I just used a font that had a different name from the "Full Name" in Get Info.

For the compilation of this answer, If the quick check above doesn't work, run this code in your project:

for (NSString *fontFamilyName in [UIFont familyNames]) {
    for (NSString *fontName in [UIFont fontNamesForFamilyName:fontFamilyName]) {
        NSLog(@"Family: %@    Font: %@", fontFamilyName, fontName);
    }
}

And search for the correct name of the font you want to use.

Swift 3.0 code:

for fontFamilyName in UIFont.familyNames{
    for fontName in UIFont.fontNames(forFamilyName: fontFamilyName){
        print("Family: (fontFamilyName)     Font: (fontName)")
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...