ios - 如何在 iOS 中使用 enumerateAttributes 递归删除 TapStops 位置
问题描述
我必须在 中显示 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>
解决方案
正如您所看到的,在我NSTextTab
通过从中减去它来减少的代码中很明显,在这种情况下defaultTabInterval
,head indentparagraphStyle
等效于modifiedTabStops.last?.location
,因为我正在更改 的两个选项卡的位置NSTextTab
,从而headIndent
解决了我的问题。这是解决方案。
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.headIndent > mutableParagraphStyle.defaultTabInterval {
if let location = modifiedTabStops.last?.location {
mutableParagraphStyle.headIndent = location
}
mutableParagraphStyle.tabStops = modifiedTabStops
}
var mutableAttributes = attributes
mutableAttributes[NSAttributedStringKey.paragraphStyle] = mutableParagraphStyle
mutableAttributedString.removeAttribute(.paragraphStyle, range: range)
mutableAttributedString.addAttribute(.paragraphStyle, value: mutableParagraphStyle, range: range)
}
})
}
推荐阅读
- html - 使用 Bootstrap 4 在 Electron 中折叠布局
- android - android.view.InflateException: Binary XML file line #34: Can't convert to dimension: type=0x12
- scripting - 红色节点上的错误“TypeError:无法读取未定义的属性‘ODP’”
- java - 带有spring boot 2.0的Postgresql模型数组对象不起作用
- java - Jpa @Converter 获取实体的ID
- javascript - 如何从功能组件调用类组件
- rust - 如何将 protobuf::parse_from_bytes 用于 Rust 结构?
- hadoop - 在 Cloudera Manager 中启动 HDFS 时出错 - 尝试绑定到“/var/hdfs-sockets/dn”时地址已在使用中
- java - 将 spring-boot 从 1.3 升级到 2.03 后出现 BeanCreationException
- uml - 类图中具有多种类型的属性