如何使用 iOS 中的 enumerateAttributes 递归地删除 TapStops 位置

How to remove TapStops location recursively using enumerateAttributes in iOS

我必须在 UITextView 中显示 HTML 数据,此 HTML 数据有时包含列表的嵌套标签,这会导致设备上出现额外的填充,我想删除额外的填充使用 swift 端,因为 CSS 的填充在这里似乎不起作用。这是我的代码和示例数据的样子

let mutableAttributedString = NSMutableAttributedString(attributedString: attributedQuestionMessage!)
    mutableAttributedString.enumerateAttributes(in: NSMakeRange(0, mutableAttributedString.length), options: []) { (attributes, range, stop) in
        let maybeParagraphStyles = attributes.filter {[=11=].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: [=11=].alignment, location: [=11=].location - mutableParagraphStyle.defaultTabInterval, options: [=11=].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>

显示这样的数据,如您所见,也存在对齐问题和填充,有什么建议吗?

正如您所看到的,在代码中很明显我通过从中减去 defaultTabInterval 来减少 NSTextTabparagraphStyle 的头部缩进相当于在这种情况下 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 {[=10=].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: [=10=].alignment, location: [=10=].location - mutableParagraphStyle.defaultTabInterval, options: [=10=].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)
            }
        })

    }