11

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

Jordan H
  • 52,571
  • 37
  • 201
  • 351
  • Can you try adding `margin:0;` and `padding:0;` in your style css? – iphonic Jun 21 '16 at 19:17
  • @iphonic Funny I just barely tried that and it didn't make a difference. – Jordan H Jun 21 '16 at 19:17
  • May be `p{margin:0; padding:0;}` ? – iphonic Jun 21 '16 at 19:21
  • I do want to preserve the margin/padding between the paragraphs though, just remove it from the last one – Jordan H Jun 21 '16 at 19:22
  • Try this `NSMutableAttributedString* mutableAttributedString=[[NSMutableAttributedString alloc] initWithData:[styledHtml dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];` then `[mutableAttributedString removeAttribute:NSParagraphStyleAttributeName range:NSMakeRange(0, mutableAttributedString.length)];` and lastly set it to label. – iphonic Jun 21 '16 at 19:23
  • When I use `p{margin:0; padding:0;}` it removes the padding between the paragraphs (and \n is still there), but the padding remains unchanged on the last `

    `, odd. Removing `NSParagraphStyleAttributeName` didn't do the trick.

    – Jordan H Jun 21 '16 at 19:25
  • @joey Did you find the solution – Deekshith Bellare Oct 06 '16 at 12:22
  • @DeekshithBellare No – Jordan H Oct 06 '16 at 13:39

2 Answers2

9

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.

vatsal rustagi
  • 166
  • 1
  • 4
3

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.

unkgd
  • 671
  • 3
  • 12