قالب وردپرس درنا توس
Home / IOS Development / How to eliminate excess line breaks from NSAttributedString (converted from HTML) to IOS UILabel object?

How to eliminate excess line breaks from NSAttributedString (converted from HTML) to IOS UILabel object?



I have some table view cells that need to show some simple html from our server. Converts html to an NSAttributedString so it can appear in UILabel objects on the cells. However, when the table view draws the cells, the UILabel objects seem to have some line breaks added at the end of them. Notice extra space here:

  Enter image description here

(Btw, the image gives two table display cells because I tried two different forms of html and in both cases seem to be in line breaks)
I thought maybe it was because of my setup that maybe UILabel was forced to its height of the setup and not free to resize / shrink itself based on the text it was assigned. But when I ship to the same label, a simple NSAttributedString created this way:

   sRet = [[NSAttributedString alloc] initWithString: @ "[Dummy text string 1
11]"];

The (yellow) UILabel is actually shrunk as:

  Enter the image description here

pretty much that shows that my layout allows UILabel to freely resize according to the text assigned to it.

Below, html is assigned to the labels and the resulting NSAttributedString values ​​when I log in to the console. These correspond to what you see in the yellow UILabel objects in the first image above. Here is html converted to an assigned string and assigned:

Student did bad

Student did ok

And here are the corresponding assigned strings:

  Student did bad
{
NSColor = "kCGColorSpaceModelRGB 0 0 0 1";
NSFont = " font-family: " TimesNewRomanPS-BoldMT  "; font-weight: bold; font-style: normal; font size: 18.00pt";
NSKern = 0;
NSParagraphStyle = "Adjustment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 22/0, LineHeightMultiple 0, LineBreakMode 0, Tabs ( n), DefaultTabInterval 36, Blocks (null), Lists (null), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 2 ";
NSStrokeColor = "kCGColorSpaceModelRGB 0 0 0 1";
NSStrokeWidth = 0;
}

Student did ok {
NSColor = "kCGColorSpaceModelRGB 0 0 0 1";
NSFont = " font-family: " Snell Roundhand  "; font-weight: normal; font style: normal; font size: 14.00pt";
NSKern = 0;
NSParagraphStyle = "Adjustment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 19/0, LineHeightMultiple 0, LineBreakMode 0, Tabs ( n), DefaultTabInterval 36, Blocks ( n) Lists ( n), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0 ";
NSStrokeColor = "kCGColorSpaceModelRGB 0 0 0 1";
NSStrokeWidth = 0;
}
{
NSColor = "kCGColorSpaceModelRGB 0 0 0 1";
NSFont = " font-family: " Times New Roman  "; font-weight: normal; font-style: normal; font size: 12.00pt";
NSKern = 0;
NSParagraphStyle = "Adjustment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 15/0, LineHeightMultiple 0, LineBreakMode 0, Tabs ( n), DefaultTabInterval 36, Blocks ( n) Lists ( n), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0 ";
NSStrokeColor = "kCGColorSpaceModelRGB 0 0 0 1";
NSStrokeWidth = 0;
}

In neither html nor the assigned strings, I see the reason that the UILabel objects have the extra vertical space. Fwiw, the pair of UILabel objects on each cell (the yellow and the white) are contained in a UIStackView, so maybe it may do it somehow. But only four restrictions, trailing, leading, bottom and top, and as mentioned above, the label can be customized so that these limitations do not seem to contribute to this.


Source link