UIFont fontWithName font name

旧时模样 提交于 2019-12-04 07:25:16

问题


Say you want a specific font for UIFont. How do you know what it's called?

E.g. if you wanted to use this code:

[someUILabelObject setFont:[UIFont fontWithName:@"American Typewriter" size:18]];

From where do you copy the exact phrase "American Typewriter". Is there a header file in Xcode?

UPDATE

Also found this handy.


回答1:


Might be interesting for you as Quick Win within the Debugger:

(lldb) po [UIFont fontNamesForFamilyName:@"Helvetica Neue"]

(id) $1 = 0x079d8670 <__NSCFArray 0x79d8670>(
HelveticaNeue-Bold,
HelveticaNeue-CondensedBlack,
HelveticaNeue-Medium,
HelveticaNeue,
HelveticaNeue-Light,
HelveticaNeue-CondensedBold,
HelveticaNeue-LightItalic,
HelveticaNeue-UltraLightItalic,
HelveticaNeue-UltraLight,
HelveticaNeue-BoldItalic,
HelveticaNeue-Italic
)

November 2018 - Update A new swift-y reference for "Custom Fonts with Xcode" - by Chris Ching. I had to update, as this is a great value posting for the new way combined with all missing parts to use custom fonts in a project.




回答2:


The documentation for UIFont is pretty clear on this:

You can use the fontNamesForFamilyName: method to retrieve the specific font names for a given font family. (Note: It is a class method)

You can get the family names like this:

NSArray *familyNames = [UIFont familyNames];



回答3:


Try

NSArray *familyNames = [UIFont familyNames];
[familyNames enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
    NSLog(@"* %@",obj);
    NSArray *fontNames = [UIFont fontNamesForFamilyName:obj];
    [fontNames enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
        NSLog(@"--- %@",obj);
    }];
}];



回答4:


I made a library to solve this problem:

https://github.com/Nirma/UIFontComplete

All fonts are represented as a system Font enum and the library also details a way of using it with your custom fonts in the read me.

Basically this:

let font = UIFont(name: "Arial-BoldItalicMT", size: 12.0)

Is replaced with either this:

let font = UIFont(font: .arialBoldItalicMT, size: 12.0)

Or this:

let myFont = Font.helvetica.of(size: 12.0)



回答5:


This is how you get all font names in your project. That's it ... 3 lines of code

NSArray *fontFamilies = [UIFont familyNames];

    for (int i=0; i<[fontFamilies count]; i++)
    {
        NSLog(@"Font: %@ ...", [fontFamilies objectAtIndex:i]);
    }



回答6:


label.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:17];


来源:https://stackoverflow.com/questions/11083954/uifont-fontwithname-font-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!