I've to show HTML data in UITextView
, this HTML data sometimes contains nested tags for lists, which cause extra padding on device, I want to remove extra padding using swift side since padding from CSS doesn't seem to be working here. Here is how my code and sample data looks like
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
this works fine, but fails for when I've to display data like this
<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>
which show data like this, as you can see there is alignment issue & padding too, any suggestions ?
As you can see it's obvious in the code that I'm reducing NSTextTab
by subtracting it defaultTabInterval
from it, head indent of paragraphStyle
is equivalent to in this case modifiedTabStops.last?.location
, since I was changing the location of both tabs of NSTextTab
, changing the headIndent
solve my problem. Here is the solution.
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)
}
})
}