ios - NSAttributedString 行间距打破 <uk> 布局

标签 ios swift nsattributedstring

我有非常简单的代码,我通过 NSAttributedStringhtml 显示为 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()

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

enter image description here

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

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

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

enter image description here

有没有简单的方法可以修复它?

谢谢

最佳答案

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

关于ios - NSAttributedString 行间距打破 <uk> 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39573719/

相关文章:

ios - 编辑mapbox-gl-native iOS SDK发出的所有HTTP请求的 header

xml - 如何显示 xmlArray 中的图像?

ios - 分别翻转字符串中的字符

swift - NSTextAttachment 色调颜色

ios - 从核心数据中获取数据

ios - TableView 行隐藏和显示为 Bool 函数

android - Worklight 7.1 使用 mapkit cordova 插件打开本地 map 应用程序

swift - 一个实体中的两个相同的 parent

ios - 我如何模仿 map 应用程序的底部工作表?

ios - 当使用 lineHeightMultiple 控制行间行距时,如何在 UITableViewCell 中垂直对齐我的 UILabel?