NSAttributedString lineSpacing brakes <ul> 布局

NSAttributedString lineSpacing brakes <ul> layout

我有非常简单的代码,我通过 NSAttributedString:

html 显示为 UILabel 的文本
let strValue = "<ul><li>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</li><li>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</li><li>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</li></ul> <b>Lorem Ipsum</b> is simply dummy text of the printing and typesetting industry."

        var attributedText : NSMutableAttributedString
        do {
            attributedText = try NSMutableAttributedString.init(data: strValue.dataUsingEncoding(NSUTF8StringEncoding)!,
                                                                options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                                                    NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding],
                                                                documentAttributes: nil)
        } catch {
            attributedText = NSMutableAttributedString(string: strValue)
        }


        label.attributedText = attributedText
        label.numberOfLines = 0
        label.lineBreakMode = NSLineBreakMode.ByWordWrapping
        label .sizeToFit()

看起来和我预期的差不多:

我需要改变的一件事是增加行间距,在我添加之后:

 let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineSpacing = 10 // TODO: font.height * multiplier
    attributedText.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:NSMakeRange(0, attributedText.length))

由于某些原因,现在第二个文本行的缩进与第一个不同?

有什么简单的方法可以修复它吗?

谢谢

根据我的经验,如果您在属性文本中使用 html,那么您必须对 html 进行此更改,html 代码之外的所有设置都将被覆盖由 html.