有时做项目会遇到这样一种需求, 控件显示的字体要求为自定义, 具体实现如下:
1. 将字体文件(.ttc, .ttf, .odf)添加到工程内:
2. 在工程内的Info.plist添加Fonts provided by application, 其下添加一个键值key -> Item 0, value -> STHeiti-Medium.ttc(字体文件名字):
3. 调用字体:
.ttf, .odf格式字体可通过fontWithName:size直接调用, .ttc格式由于为字体集, 可通过遍历字体集获取所需字体(Font)来调用:
NSArray *familyNames = [UIFont familyNames]; for( NSString *familyName in familyNames ) { printf( "Family: %s ", [familyName UTF8String] ); NSArray *fontNames = [UIFont fontNamesForFamilyName:familyName]; for( NSString *fontName in fontNames ) { printf( " Font: %s ", [fontName UTF8String] ); } }
所需字体(Font):
设置字体:
_label1.font = [UIFont fontWithName:@"STHeitiTC-Medium" size:20];