I have pretty simple code where i'm displaying html
as UILabel
's text via NSAttributedString
:
let strValue = "<ul><li>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</li><li>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</li><li>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</li></ul> <b>Lorem Ipsum</b> is simply dummy text of the printing and typesetting industry."
var attributedText : NSMutableAttributedString
do {
attributedText = try NSMutableAttributedString.init(data: strValue.dataUsingEncoding(NSUTF8StringEncoding)!,
options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding],
documentAttributes: nil)
} catch {
attributedText = NSMutableAttributedString(string: strValue)
}
label.attributedText = attributedText
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.ByWordWrapping
label .sizeToFit()
It looks pretty much as I expected:
One thing I need to change is to increase line spacing, and after I added:
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 10 // TODO: font.height * multiplier
attributedText.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:NSMakeRange(0, attributedText.length))
For some reasons now second text line has no same indent as first one?
Is there easy way I can fix it?
Thanks
In my experience if you're using html in your attributed text then you have to make this change on the html all your settings outside the html code will be overridden by the html.