首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >Xcode:在动态框架中使用自定义字体

Xcode:在动态框架中使用自定义字体
EN

Stack Overflow用户
提问于 2015-05-28 21:25:49
回答 6查看 15.4K关注 0票数 40

我在框架中添加了自定义字体。我遵循了所有的步骤,但它不起作用。

我可以在Interface Builder中设置字体,但当我构建项目时,它不会在模拟器/设备上显示此字体。

EN

回答 6

Stack Overflow用户

发布于 2015-10-01 00:11:19

以下是我对John的回答,展示了如果您有很多字体时如何调用该函数

代码语言:javascript
代码运行次数:0
运行
复制
import Foundation

extension UIFont {

    @nonobjc static var loadAllFontsDO: dispatch_once_t = 0

    class func initialsAvatarFont() -> UIFont {
        loadAllFonts()
        if let retval = UIFont(name: "MyFontName", size: kInitialsAvatarFontSize) {
            return retval;
        } else {
            return UIFont.systemFontOfSize(kInitialsAvatarFontSize)
        }
    }

    class func loadAllFonts() {
        dispatch_once(&loadAllFontsDO) { () -> Void in
            registerFontWithFilenameString("thefontfilename.ttf", bundleIdentifierString: "nameOfResourceBundleAlongsideTheFrameworkBundle")
            // Add more font files here as required
        }
    }

    static func registerFontWithFilenameString(filenameString: String, bundleIdentifierString: String) {
        let frameworkBundle = NSBundle(forClass: AnyClassInYourFramework.self)
        let resourceBundleURL = frameworkBundle.URLForResource(bundleIdentifierString, withExtension: "bundle")
        if let bundle = NSBundle(URL: resourceBundleURL!) {
            let pathForResourceString = bundle.pathForResource(filenameString, ofType: nil)
            let fontData = NSData(contentsOfFile: pathForResourceString!)
            let dataProvider = CGDataProviderCreateWithCFData(fontData)
            let fontRef = CGFontCreateWithDataProvider(dataProvider)
            var errorRef: Unmanaged<CFError>? = nil

            if (CTFontManagerRegisterGraphicsFont(fontRef!, &errorRef) == false) {
                NSLog("Failed to register font - register graphics font failed - this font may have already been registered in the main bundle.")
            }
        }
        else {
            NSLog("Failed to register font - bundle identifier invalid.")
        }
    }
}
票数 16
EN

Stack Overflow用户

发布于 2015-05-28 23:41:09

通过在框架中实现+load方法,您可以从动态框架中加载和使用捆绑的自定义字体。

load方法中,找到捆绑包中的字体,然后注册它们。这使得它们对应用程序可用,而不必在主项目中指定它们。

代码语言:javascript
代码运行次数:0
运行
复制
+ (void)load
{
    static dispatch_once_t onceToken;
     dispatch_once(&onceToken, ^{
        // Dynamically load bundled custom fonts

        [self bible_loadFontWithName:kBIBLECustomFontBoldName];
        [self bible_loadFontWithName:kBIBLECustomFontBoldItalicName];
        [self bible_loadFontWithName:kBIBLECustomFontItalicName];
        [self bible_loadFontWithName:kBIBLECustomFontRegularName];
    });
}

+ (void)bible_loadFontWithName:(NSString *)fontName
{
     NSString *fontPath = [[NSBundle bundleForClass:[BIBLE class]] pathForResource:fontName ofType:@"otf"];
     NSData *fontData = [NSData dataWithContentsOfFile:fontPath];

     CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)fontData);

     if (provider)
     {
        CGFontRef font = CGFontCreateWithDataProvider(provider);

         if (font)
         {
             CFErrorRef error = NULL;
             if (CTFontManagerRegisterGraphicsFont(font, &error) == NO)
             {
                 CFStringRef errorDescription = CFErrorCopyDescription(error);
                 NSLog(@"Failed to load font: %@", errorDescription);
                 CFRelease(errorDescription);
             }

             CFRelease(font);
        }

        CFRelease(provider);
    }
}
票数 9
EN

Stack Overflow用户

发布于 2020-08-17 20:20:44

以下解决方案允许您自动加载具有特定扩展名的所有字体:

代码语言:javascript
代码运行次数:0
运行
复制
static func registerFonts() {
    let fonts = Bundle(for: 
OrientationMonitor.self).urls(forResourcesWithExtension: "ttf", subdirectory: nil)
    fonts?.forEach({ url in
        CTFontManagerRegisterFontsForURL(url as CFURL, .process, nil)
    })
}

请确保将OrientationMonitor替换为框架中现有的类。

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30507905

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档