有没有办法在NSAttributedString中限制段落中的行数
Im在NSAttributedString中追加了两个字符串,我希望它们最多为3行,第一个字符串将是1-2行,如果需要的话将被截断。第二个字符串应该总是在最后一行。
类似于:
this is my first string
if its too long i't will get trun...
But this is my second string我所做的是:
// First string
NSAttributedString *first = [[NSAttributedString alloc] initWithString:@"this is my first string if its too long i't will get trunticated"
attributes:@{NSForegroundColorAttributeName:[UIColor redColor],
NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue-Light" size:17.0]];
[str appendAttributedString:first];
// New line
[str appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
// Add photo count
NSAttributedString *second = [[NSAttributedString alloc] initWithString:@"But this is my second string"
attributes:@{NSForegroundColorAttributeName:[UIColor redColor],
NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue-Light" size:14.0]}];
[str appendAttributedString:second];但结果是:
this is my first string
if its too long i't will get
trunticated第一个字符串接受前3行,并将第二个字符串从标签中推开。
如何将第一个字符串段落限制为2行?
发布于 2015-12-29 20:27:26
您可以计算图形组件(UITextView或UITextField)所能处理的字母数量,可以使用大写字母和更大宽度的字母反复查看。比,使用:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{}检查每个输入,是否数量足够,或它是否仍然可以用于更多的字母。创建字符限制,并在每次调用此方法时将其减少。
发布于 2015-07-15 17:24:50
用一个约束限制行数!
只需在您的NSLayoutConstraint上添加一个具有以下值的UILabel:
请参见故事板集成:

https://stackoverflow.com/questions/28438016
复制相似问题