限制NSAttributedString行数

Ita*_*mza 7 nsattributedstring ios nsparagraphstyle

有没有办法限制NSAttributedString段落中的行数?
我在NSAttributedString中追加两个字符串,我希望它们最多3行,第一个字符串将是1-2行,如果需要则截断.第二个字符串应该总是在最后一行上
:

this is my first string
if its too long i't will get trun...
But this is my second string
Run Code Online (Sandbox Code Playgroud)

我做的是:

    // 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];
Run Code Online (Sandbox Code Playgroud)

但结果是:

this is my first string
if its too long i't will get
trunticated
Run Code Online (Sandbox Code Playgroud)

第一个字符串占用前3行并将第二个字符串推出标签.
如何将第一个字符串段限制为2行?

小智 0

用一个约束限制行数!

只需在 UILabel 上添加 NSLayoutConstraint 并使用以下值:

  • attribute = NSLayoutAttributeHeight(故事板中的“高度”)
  • 关系= NSLayoutRelationLessThanOrEqual(情节提要中的“小于或等于”)
  • 常量 = 所需行数的高度

请参阅情节提要集成:

查看故事板集成