Xcode:在Dynamic框架中使用自定义字体 [英] Xcode: Using custom fonts inside Dynamic framework

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

问题描述

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

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

解决方案

我在这里有点晚了,但是我拿了PetahChristian的解决方案并创建了一个Swift版本以扩展的形式。这对我有用。我发现,当您尝试使用字体名称和大小使用常规方式获取字体时,它总是查看字体文件的主包,并且没有将包标识符作为参数的方法。如果苹果公司制造出这样的产品会很好。



Swift:

  public extension UIFont {

public static func registerFontWithFilenameString(filenameString:String,bundle:Bundle){
$ b $ guard let pathForResourceString = bundle.path( forResource:filenameString,ofType:nil)else {
print(UIFont +:未能注册字体 - 未找到资源的路径)
返回
}

警告let fontData = NSData(contentsOfFile:pathForResourceString)else {
print(UIFont +:无法注册字体 - 字体数据无法加载。)
return
}

guard让dataProvider = CGDataProvider(data:fontData)else {
print(UIFont +:无法注册字体 - 数据提供者无法加载。)
return
}

guard让fontRef = CGFont(dataProvider)else {
print(UIFont +:无法注册字体 - fo nt无法加载。)
返回
}

var errorRef:非托管< CFError> ;? =无
if(CTFontManagerRegisterGraphicsFont(fontRef,& errorRef)== false){
print(UIFont +:注册字体失败 - 注册图形字体失败 - 此字体可能已经注册在main )
}
}


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

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.

解决方案

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.

Swift:

public extension UIFont {

public static func registerFontWithFilenameString(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 fontRef = CGFont(dataProvider) else {
        print("UIFont+:  Failed to register font - font could not be loaded.")
        return
    }

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆