iOS从服务器加载字体文件和使用字体

来源:互联网 发布:banner轮播js代码 编辑:程序博客网 时间:2024/06/05 06:17

iOS可以加载其他字体库文件。

1,各种字体信息存在服务器上,包括字体文件下载地址,字体名称等信息。

2,更具字体文件的地址,下载字体文件到本地。

3,加载使用字体。
    
    主要代码:更具url加载使用字体
    +(UIFont*)customFontWithFontUrl:(NSURL*)customFontUrl size:(CGFloat)size
{
    NSURL *fontUrl = customFontUrl;
    CGDataProviderRef fontDataProvider = CGDataProviderCreateWithURL((__bridge CFURLRef)fontUrl);
    CGFontRef fontRef = CGFontCreateWithDataProvider(fontDataProvider);
    CGDataProviderRelease(fontDataProvider);
    CFErrorRef error;
    bool isSuccess = CTFontManagerRegisterGraphicsFont(fontRef, &error);
    if(!isSuccess){
        //如果注册失败,则不使用
        CFStringRef errorDescription = CFErrorCopyDescription(error);
        NSLog(@"Failed to load font: %@", errorDescription);
        CFRelease(errorDescription);
    }
    NSString *fontName = CFBridgingRelease(CGFontCopyPostScriptName(fontRef));
    UIFont *font = [UIFont fontWithName:fontName size:size];
    CGFontRelease(fontRef);
    return font;

}


效果截图:




源码地址:点击打开链接



0 0
原创粉丝点击