Xcode:在Dynamic框架中使用自定义字体

编程入门 行业动态 更新时间:2024-10-25 10:21:56
本文介绍了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框架中使用自定义字体

本文发布于:2023-11-07 22:34:17,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   框架   字体   Xcode   Dynamic

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!