本文介绍了Xcode:在动态框架中使用自定义字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在框架中添加了自定义字体.我按照所有步骤操作,但它不起作用.

I added custom font to a framework. I followed all the steps, but it doesn't work.

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

I am able to set the font in Interface Builder, but when I build the project it doesn't show this font on the simulator/device.

推荐答案

我来的有点晚了,但是我采用了 PetahChristian 的解决方案,并以扩展的形式创建了一个 Swift 版本.这对我有用.我发现,当您尝试使用字体名称和大小使用常规方式获取字体时,它总是在字体文件的主包中查找,并且没有将包标识符作为参数的方法.如果苹果能做出来就好了.

I'm here a bit late, but I took PetahChristian's solution and created a Swift version in the form of an extension. This is working for me. I've found that when you try to get a font using a font name and a size using the regular way that it always looks in the main bundle for the font file, and there's no method that takes a bundle identifier as a parameter. It would be nice if Apple would make one.

斯威夫特:

public extension UIFont {

    public static func jbs_registerFont(withFilenameString filenameString: String, bundle: Bundle) {

        guard let pathForResourceString = bundle.path(forResource: filenameString, ofType: nil) else {
            print("UIFont+:  Failed to register font - path for resource not found.")
            return
        }

        guard let fontData = NSData(contentsOfFile: pathForResourceString) else {
            print("UIFont+:  Failed to register font - font data could not be loaded.")
            return
        }

        guard let dataProvider = CGDataProvider(data: fontData) else {
            print("UIFont+:  Failed to register font - data provider could not be loaded.")
            return
        }

        guard let font = CGFont(dataProvider) else {
            print("UIFont+:  Failed to register font - font could not be loaded.")
            return
        }

        var errorRef: Unmanaged<CFError>? = nil
        if (CTFontManagerRegisterGraphicsFont(font, &errorRef) == false) {
            print("UIFont+:  Failed to register font - register graphics font failed - this font may have already been registered in the main bundle.")
        }
    }

}

用法示例:

UIFont.jbs_registerFont(
    withFilenameString: "Boogaloo-Regular.ttf",
    bundle: Bundle(identifier: "com.JBS.JBSFramework")!
)

这篇关于Xcode:在动态框架中使用自定义字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 08:10