</pre><pre name="code" class="objc">
1增加代码
@implementation UIFont (custom)
+(UIFont*)customFontWithttfFileName:(NSString*)fileName Size:(NSInteger)size
{
NSString *fontPath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"ttf"];
if (!fontPath) {
fontPath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"TTF"];
}
NSURL *url = [NSURL fileURLWithPath:fontPath];
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithURL((__bridge CFURLRef)url);
if (fontDataProvider == NULL) return nil;
CGFontRef newFont = CGFontCreateWithDataProvider(fontDataProvider);
CGDataProviderRelease(fontDataProvider);
if (newFont == NULL) return nil;
NSString *fontName = (__bridge NSString *)CGFontCopyFullName(newFont);
UIFont *font = [UIFont fontWithName:fontName size:size];
CGFontRelease(newFont);
return font;
}
@end
2,设置 info-plist
Fonts provided by application
3,如果想让storyboard中的UIlabel资源字体更换成自定义字体,同时保持字体大小
@implementation NSObject (customFont)
@dynamic font;
- (NSString *)fontName
{
return self.font.fontName;
}
- (void)setFontName:(NSString *)fontName
{
self.font = [UIFont customFontWithttfFileName:@"兰亭细黑GBK" Size:self.font.pointSize];
}
@end