2

IOS 自定义字体

 2 years ago
source link: https://www.isaced.com/post-211.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

IOS 自定义字体

今天项目中自带的字体已经满足不了了,那么只有自定义字体,再加上iOS原生的字体对中文、日文等支持都不完美,所以还是得重新弄个字体上去。

关于自定义字体网上有很多文章,这里只做收藏之用。

自定义字体首先得有一个字体文件,一般以.ttf结尾,经测试.otf也是可以的。

具体操作方法如下:

首先,在工程中的XXX-Info.plist文件中添加一行(Add Row),输入”UIAppFonts“,回车后XCode会自动给你转换为”Fonts provided by application”,类型为Array就可以了,Value类型为String,写入拖到工程里的字体文件名,如:”epgyobld.ttf”。

这里需要注意的是,一般字体名不是字体文件名,那么怎么才能知道刚才添加的字体的字体名呢?

遍历一下系统中的字体就可以了,代码如下:

    NSArray *familyNames = [UIFont familyNames];
    for( NSString *familyName in familyNames ){
        
        printf( "Family: %s \n", [familyName UTF8String] );
        
        NSArray *fontNames = [UIFont fontNamesForFamilyName:familyName];
        for( NSString *fontName in fontNames ){
            printf( "\tFont: %s \n", [fontName UTF8String] );
        }
    }

然后在XCode中的All OutPut窗口中看到字体列表,找到自己的Font就可以了。

这里有一个小技巧就是在OutPut窗口中按Command+F健可以查找,输入字体名近似的关键字,很快就能找到。

9987474473fd02775cd301605f2bc223.png

字体添加进工程,FontName也知道了,那就可以直接用了:

yinLabel.font = [UIFont fontWithName:@"AxisStd-ExtraLight" size:250.0];

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK