Как рекурсивно удалить местоположение TapStops с помощью enumerateAttributes в iOS - PullRequest
0 голосов
/ 18 мая 2018

Я должен показывать данные HTML в UITextView, эти данные HTML иногда содержат вложенные теги для списков, которые вызывают дополнительное заполнение на устройстве, я хочу удалить дополнительное заполнение, используя быструю сторону, так как заполнение из 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>

, которые показывают данные, подобные этим,Как вы можете видеть, есть проблема выравнивания и заполнения, какие-либо предложения?enter image description here

1 Ответ

0 голосов
/ 20 мая 2018

Как вы можете видеть в коде, очевидно, что я уменьшаю NSTextTab, вычитая из него defaultTabInterval, отступ в paragraphStyle эквивалентен в этом случае 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)
            }
        })

    }
...