【问题标题】:Changing Font for Unordered List for Swift NSAttributedString为 Swift NSAttributedString 更改无序列表的字体
【发布时间】:2018-09-13 05:53:25
【问题描述】:

我正在尝试使用 HTML 字符串创建 NSAttributedString,但我必须更改字符串的字体。我能够通过以下方式成功实现这一目标:

let myOriginalString: String = "<!DOCTYPE html>
                    <head>
                      <style type=\"text/css\">
                        body{font-family: '-apple-system','Avenir-Medium',`Avenir-Black`; font-size:14;}
                      </style>
                    <body>
                      My HTML content
                    </body>"

let data: Data = myOriginalString.data(using: .utf8)

let myConvertedString: NSAttributedString = NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)

myConvertedString 在屏幕上呈现时,文本的字体是正确的。

但是当我尝试在 HTML 正文包含 无序列表 的情况下进行相同的转换时,无序列表中的内容不是我指定的格式:

let myOriginalString: String = "<!DOCTYPE html>
                    <head>
                      <style type=\"text/css\">
                        body{font-family: '-apple-system','Avenir-Medium',`Avenir-Black`; font-size:14;}
                      </style>
                    <body>
                      My HTML content
                      <ul>
                        <li>Item 1</li>
                        <li>Item 2</li>
                      </ul>
                    </body>"

let data: Data = myOriginalString.data(using: .utf8)

let myConvertedString: NSAttributedString = NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)

myConvertedString在屏幕上渲染时,字符串My HTML Content的格式是正确的,但是Item 1Item 2不是我指定的字体。

我尝试为ul 标签和li 标签添加样式,但结果相同。

为什么 NSAttributedString 用正确的字体渲染所有文本,除了无序列表?有没有更好的方法来创建一个使用 NSAttributedString 而不是使用 html 的无序列表?

【问题讨论】:

  • 也许每个项目都缺少结束标签会以某种方式搞砸?
  • 糟糕,抱歉,这是一个 typ0。我的实际代码中有结束标签。我已经解决了这个问题。
  • 您的 CSS 无效(除非是拼写错误):将反引号替换为 Avenir-Black 周围的单引号;并给font-size一个单位:font-size: 14pt;

标签: html ios swift html-lists nsattributedstring


【解决方案1】:

我找到了一个比使用 NSMutableParagraphStyle 代替 HTML 无序列表更好的解决方案:

let myAttributedString = NSMutableAttributedString()

// Create a paragraph style
let paragraphStyle: NSMutableParagraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
            paragraphStyle.tabStops = [NSTextTab(textAlignment: .left, location: 10, options: [:])]
            paragraphStyle.firstLineHeadIndent = 0
            paragraphStyle.headIndent = 10

let item1 =  NSMutableAttributedString(string: "\u{2022} Item1\n\n", attributes: [.font: UIFont.systemFont(ofSize: 14.0), .paragraphStyle: paragraphStyle])

let item2 =  NSMutableAttributedString(string: "\u{2022} Item2\n\n", attributes: [.font: UIFont.systemFont(ofSize: 14.0), .paragraphStyle: paragraphStyle])

myAttributedString.append(item1)
myAttributedString.append(item2)

现在 myAttributedString 以正确的字体和正确的样式打印出 item1item2(包装后的单词与项目符号点的 minx 值不同)

我认为 HTML 部分出现了不正确的字体,因为我试图创建一个 NSAttributedString,它包含一些正常的 NSAttributedString、一个 HTML NSAttributedString,然后是更正常的 NSAttributedString。我不认为这应该是一个问题,也许这是一个错误???

【讨论】:

    【解决方案2】:

    我刚用 Arial 试了一下,效果很好:

    override func viewDidLoad() {
            super.viewDidLoad()
    
            let myOriginalString = """
            <!DOCTYPE html>
            <head>
            <style type="text/css">
            body {
                font-family: Arial;
                font-size: 32px;
                color: red;
            }
            </style>
            <body>
            My HTML content
            <ul>
                <li>Item 1</li>
                <li>Item 2</li>
            </ul>
            </body>
            """
    
            if let data = myOriginalString.data(using: .utf8) {
    
                if let converted = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil) {
    
                    textView.attributedText = converted
                }
            }
        }
    

    【讨论】:

    • 尝试在converted NSAttributedString 中添加两个(一个之前和一个之后)非 html 样式的 NSAttributedString,看看是否仍然得到相同的结果...
    猜你喜欢
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    • 1970-01-01
    • 2019-02-19
    • 1970-01-01
    • 1970-01-01
    • 2012-10-10
    • 1970-01-01
    相关资源
    最近更新 更多