• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

iphone - 在 iOS 中,是否可以在运行时向应用程序添加字体?

[复制链接]
菜鸟教程小白 发表于 2022-12-11 22:16:22 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

在我的应用程序中,我想让用户下载和使用字体,但我不知道如何动态执行此操作。我知道我们必须在应用程序的 Info.plist 文件中指定我们想要使用的字体,但我们无法以编程方式向该 plist 文件添加任何内容。有 zynga 的库,但它是 UILabel 的子类。

请帮忙



Best Answer-推荐答案


是的,有可能:

/**
 Downloads a `.ttf` file from an URL and creates an UIFont.
 
 - Parameter url: The URL of the `.ttf` file.
 
 - Precondition: The URL Session must be previously configured at ease.
 */
fileprivate func setFont(url: URL) {
    // Download the font file in .ttf format.
    dataTask = urlSession?.dataTask(with: url,
                                    completionHandler: { [weak self] (data, response, error) in
        guard error == nil,
            let data = data,
            (response as? HTTPURLResponse)?.statusCode == 200,
            // Create a Font Descriptor from the raw data.
            let ctFontDescriptor = CTFontManagerCreateFontDescriptorFromData(data as CFData) else {
                // Error during the download of the .ttf file.
                self?.error()
                return
        }
        // Create CTFont from the Font Descriptor and convert it to UIFont.
        let font: UIFont = CTFontCreateWithFontDescriptor(ctFontDescriptor, 0.0, nil) as UIFont
        // Do whatever you want with the UIFont 
        self?.success(font: font)
    })
    dataTask?.resume()
}

使用此代码,您可以从 Internet 下载包含字体的 .ttf 文件(您可以从任何其他来源获取该文件)并将其用作 UIFont。

关于iphone - 在 iOS 中,是否可以在运行时向应用程序添加字体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9821211/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap