我必须在 中显示 HTML 数据UITextView
,此 HTML 数据有时包含列表的嵌套标签,这会导致设备上出现额外的填充,我想使用 swift 端删除额外的填充,因为来自 CSS 的填充似乎在这里不起作用。这是我的代码和示例数据的样子
let mutableAttributedString = NSMutableAttributedString(attributedString: attributedQuestionMessage!)
mutableAttributedString.enumerateAttributes(in: NSMakeRange(0, mutableAttributedString.length), options: []) { (attributes, range, stop) in
let maybeParagraphStyles = attributes.filter {$0.key == NSAttributedStringKey.paragraphStyle }
maybeParagraphStyles.forEach({ (style) in
if let paragraphStyle = style.value as? NSParagraphStyle {
let mutableParagraphStyle = paragraphStyle.mutableCopy() as! NSMutableParagraphStyle
let modifiedTabStops = mutableParagraphStyle.tabStops.map {
NSTextTab(textAlignment: $0.alignment, location: $0.location - mutableParagraphStyle.defaultTabInterval, options: $0.options)
}
if modifiedTabStops.count > 0 {
mutableParagraphStyle.tabStops = modifiedTabStops
}
var mutableAttributes = attributes
mutableAttributes[NSAttributedStringKey.paragraphStyle] = mutableParagraphStyle
mutableAttributedString.removeAttribute(.paragraphStyle, range: range)
mutableAttributedString.addAttribute(.paragraphStyle, value: mutableParagraphStyle, range: range)
}
})
}
textView.attributedText = mutableAttributedString
这工作正常,但当我必须显示这样的数据时失败
<html> <head> <style type="text/css">
body { font-size: 14px; font-family: -apple-system,
Arial, sans-serif; color: black; margin-bottom: 0px;
padding-bottom: 0px; line-height: 20px; } </style> </head>
<body> <p>To make this more tangible, let’s bring this concept to
the team level. What would you say are the aspirations for your
team?</p>
<ul><ul type="disc"><li>What is the overall goal?
<ul><ul style="list-style-type:circle"><li>E.g., Increase customer
loyalty by 20% as measured by Repeat Customer Rate</li></ul></ul></li>
<li>What specific initiatives do you think would lead you to achieve
it?<ul><ul style="list-style-type:circle"><li>E.g., Implement a loyalty
program that rewards customers each time they purchase a certain value
of items</li></ul></ul></li></ul></ul> </body> </html>