0

The dots are added automatically by the UILabel, cause of line limitation, but they get the background color of hidden truncated text:

So I have UILabel with line limit of 10 and line break mode of TruncatingTail. I also have 2 types of attributed strings that build this UILabel content.

  1. NSForegroundColorAttributeName, NSFontAttributeName
  2. NSBackgroundColorAttributeName, NSForegroundColorAttributeName, NSFontAttributeName

Any idea why the UILabel is adding background color to the dots? There is text in line 12 (which is truncated) that have that background...

Enamul Hassan
  • 5,266
  • 23
  • 39
  • 56
Tziki
  • 311
  • 1
  • 8

2 Answers2

0

=Here Given Code Try it..

NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"test"];

NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];

style.lineBreakMode = NSLineBreakByTruncatingTail;

[text addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, text.length)];

label.attributedText = text;

Akash
  • 461
  • 2
  • 14
0

It looks like your NSAttributedString styling is picking up on an instance of "Windsor" that is past the truncation point.

You could find where the truncation occurs, and then only apply the text attribute to the range of the string up to the truncation point.

See this SO answer to calculate this range.

NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.lineBreakMode = mylabel.lineBreakMode;
NSDictionary *attributes = @{NSFontAttributeName : mylabel.font,
                             NSParagraphStyleAttributeName : paragraph};
CGSize constrainedSize = CGSizeMake(mylabel.bounds.size.width, NSIntegerMax);
CGRect rect = [mylabel.text boundingRectWithSize:constrainedSize
                                         options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                      attributes:attributes context:nil];
if (rect.size.height > mylabel.bounds.size.height) {
    NSLog(@"TOO MUCH");
}
Community
  • 1
  • 1
ackerman91
  • 549
  • 3
  • 13
  • Nice idea. The only problem that the label size changes all the time so I'll have to do that calculation too many times and it is expensive. As you can see it does picking the background color but not the font color. This smells like a bug and I wonder if anyone managed to figure it out – Tziki Jan 14 '16 at 06:24
  • That's looks like a bug so I just highlight the text with different text color and not foreground color. – Tziki Mar 17 '16 at 10:49