4

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

4

2 回答 2

3

尝试使用UILabel而不是UITextView. 在截断新行方面似乎表现得更好。

于 2016-04-12T15:40:24.117 回答
1

如果您还没有尝试随机的事情,那甚至不是解决方案,而是可能的解决方法,并且可能无论如何都不起作用,但无论如何:

  • 更改maximumNumberOfLines0并依赖框架/自动布局来调整文本视图及其容器的大小,可能与将文本容器的heightTracksTextView属性设置为true

  • 在仅包含换行符的行的开头插入水平空格或不可见字符#hacky

于 2016-04-07T17:02:39.507 回答