iOS添加自定义字体

</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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值