iosuitextviewnewlinetruncationnstextcontainer

NSLineBreakByTruncatingTail on UITextView removing new line breaks


I have a UITextView with attributedText that has multiple new line breaks included in its attributed string.

NSMutableAttributedString *info = [[NSMutableAttributedString alloc] initWithString:@""];

NSAttributedString *title = [[NSAttributedString alloc] initWithString: [NSString stringWithFormat:@"%@\n", item.title] attributes:titleAttributes];

NSAttributedString *description = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n\n", description] attributes:descriptionAttributes]

[info appendAttributedString:bioItemTitle];
[info appendAttributedString:bioItemDescription];

textView.attributedText = info;

I've set the lineBreakMode of the textView's textContainer to NSLineBreakByTruncatingTail.

textView.textContainer.lineBreakMode = NSLineBreakByTruncatingTail;

The textView's textContainer also has a maximum number of lines.

textView.textContainer.maximumNumberOfLines = 8;

The problem arises when the 8th line of the textView is a new line, and not a line of characters. The textContainer truncates by removing the new line and replacing it with the next line of written characters.

How do I preserve the new line while still setting a lineBreakMode?

See screenshots


Solution

  • Try using UILabel instead of UITextView. It seems to play nicer in regard to truncating a new line.