Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove padding under last paragraph from NSAttributedString created from HTML using NSHTMLTextDocumentType

When creating an NSAttributedString from HTML, using NSHTMLTextDocumentType, I'm finding it will add an \n for each paragraph even after the last paragraph. This is adding undesired padding underneath the last paragraph of text that's shown in a UILabel. How does one remove that extra padding for the last paragraph only?

NSString *style = @"<style> body { font-family: Avenir; font-size: 18px; color: blue; } p:last-of-type { margin: 0; }</style>";
NSString *html = @"<p>A whole bunch of sample text goes right here.</p><p>Now here's another paragraph that unfortunately has an extra line underneath the text adding undesired padding to the label. :(</p>";
NSString *styledHtml = [NSString stringWithFormat:@"%@%@", style, html];

self.label.attributedText = [[NSMutableAttributedString alloc] initWithData:[styledHtml dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];

enter image description here enter image description here

like image 627
Jordan H Avatar asked Jun 21 '16 19:06

Jordan H


2 Answers

Swift 4 version:

Since you are using an NSMutableAttributedString object you can remove the newline character at the end (if it exists) like this:

if let lastCharacter = attrStr.string.last, lastCharacter == "\n" {
    attrStr.deleteCharacters(in: NSRange(location: attrStr.length-1, length: 1))
}

The reason for the extra newline character seems to originate from the way xmllib processes the html. It wraps the "tagless" string into a <p> tag and the tag adds a newline character by default.

like image 87
vatsal rustagi Avatar answered Nov 09 '22 12:11

vatsal rustagi


Don't know if its still relevant, when I checked the NSAttributedString output in these cases, I saw that <p> tag adds characters after every closure with some default font settings:

{
NSColor = "kCGColorSpaceModelRGB 1 1 1 1 ";
NSFont = "<UICTFont: 0x7f94e932a720> font-family: \"Times New Roman\"; font-weight: normal; font-style: normal; font-size: 1.00pt";
NSKern = 0;
NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 20/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (\n), Lists (\n), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0";
NSStrokeColor = "kCGColorSpaceModelRGB 1 1 1 1 ";
NSStrokeWidth = 0;

}

So instead of using the <p> tag, I wrapped everything with a <span> tag:

NSString *html = @"<span style=\"[STYLE CAN BE ADDED HERE]\">A whole bunch of sample text goes right here.</span><br /><span>Now here's another paragraph that unfortunately has an extra line underneath the text adding undesired padding to the label. :(</span>";

Kind of a work-around but it does the trick.

like image 44
unkgd Avatar answered Nov 09 '22 12:11

unkgd