gpt4 book ai didi

ios - NSAttributedString 行间距打破 布局

转载 作者:行者123 更新时间:2023-11-28 06:33:47 25 4
gpt4 key购买 nike

我有非常简单的代码,我通过 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/

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