我在框架中添加了自定义字体.我遵循了所有的步骤,但都不起作用.
我可以在Interface Builder中设置字体,但当我构建项目时,它不会在模拟器/设备上显示此字体.
我在框架中添加了自定义字体.我遵循了所有的步骤,但都不起作用.
我可以在Interface Builder中设置字体,但当我构建项目时,它不会在模拟器/设备上显示此字体.
我来晚了一点,但我接受了PetahChristian的解决方案,以扩展的形式创建了一个Swift版本.这对我有用.我发现,当你试图以常规方式使用字体名称和大小获取字体时,它总是在主Bundle 包中查找字体文件,并且没有方法将Bundle 包标识符作为参数.如果苹果能做一个就好了.
Swift:
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")!
)