首页 > 解决方案 > 如何在 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>

显示这样的数据,您可以看到还有对齐问题和填充,有什么建议吗? 在此处输入图像描述

标签: iosswiftalignmentnsattributedstringnstexttab

解决方案


正如您所看到的,在我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)
            }
        })

    }

推荐阅读