更改Swift NSAttributedString的无序列表的字体

时间:2018-05-22 20:16:03

标签: html ios swift html-lists nsattributedstring

我正在尝试从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创建无序列表?

2 个答案:

答案 0 :(得分:0)

我刚用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
            }
        }
    }

答案 1 :(得分:0)

我发现使用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,后面是更正常的NSAttributedStrings。我不认为这应该是一个问题,也许这是一个错误???