gpt4 book ai didi

ios - 在 iOS 应用程序中添加自定义字体大小选项

转载 作者:行者123 更新时间:2023-11-28 13:37:41 26 4
gpt4 key购买 nike

我正在开发一个应用程序,这个应用程序中有很多文本文件。用户点击一个按钮,一个文件被加载到新屏幕上。

此文件仅包含文本,但有几行字体大小为 15,其他行字体大小为 10。用户可以从该文件的设置中更改字体大小。

加载文件以便用户增大或减小字体大小的正确方法是什么?

我认为属性文本不是正确的选择。

最佳答案

在这个用例中,NSAttributedString 是我最好的 friend ,我会和他一起去。

话虽如此,您不必在代码文件中保留大文本,保留文件

  1. 在应用程序包中,您可以使用从应用程序包加载文件

    if let path = Bundle.main.path(forResource: "file1", ofType: "txt"), let text = try? String(contentsOfFile: path) {
    ...
    }
  2. 在远程位置,您可以使用从远程 URL 加载字符串

    if let url = URL(string: "https://www.example.org"), let text = try? String(contentsOf: url) {
    ...
    }

Add option for custom font size

您可以在您的文本中添加 HTML 标签并使用 CSS 样式,这里是一个例子

let headerFont = UIFont(name: "ArialRoundedMTBold", size: 30)!
let headerStyle = ".heading{font-family:\(headerFont.fontName);font-size:\(Int(headerFont.pointSize));color:#000}"

let commonFont = UIFont(name: "ArialMT", size: 22)!
let commonStyle = "*{font-family:\(commonFont.fontName);font-size:\(Int(commonFont.pointSize));color:#666}"

let htmlData = Data("<style>\(commonStyle)\(headerStyle)</style>\(text)".utf8)
let attributedString = try? NSAttributedString(data: htmlData, options: [
.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue
], documentAttributes: nil)

关于ios - 在 iOS 应用程序中添加自定义字体大小选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56404650/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com