首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >限制NSAttributedString行数

限制NSAttributedString行数
EN

Stack Overflow用户
提问于 2015-02-10 17:29:00
回答 2查看 5K关注 0票数 8

有没有办法在NSAttributedString中限制段落中的行数

Im在NSAttributedString中追加了两个字符串,我希望它们最多为3行,第一个字符串将是1-2行,如果需要的话将被截断。第二个字符串应该总是在最后一行。

类似于:

代码语言:javascript
复制
this is my first string
if its too long i't will get trun...
But this is my second string

我所做的是:

代码语言:javascript
复制
    // 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];

但结果是:

代码语言:javascript
复制
this is my first string
if its too long i't will get
trunticated

第一个字符串接受前3行,并将第二个字符串从标签中推开。

如何将第一个字符串段落限制为2行?

EN

回答 2

Stack Overflow用户

发布于 2015-12-29 20:27:26

您可以计算图形组件(UITextView或UITextField)所能处理的字母数量,可以使用大写字母和更大宽度的字母反复查看。比,使用:

代码语言:javascript
复制
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{}

检查每个输入,是否数量足够,或它是否仍然可以用于更多的字母。创建字符限制,并在每次调用此方法时将其减少。

票数 1
EN

Stack Overflow用户

发布于 2015-07-15 17:24:50

用一个约束限制行数!

只需在您的NSLayoutConstraint上添加一个具有以下值的UILabel:

  • 属性= NSLayoutAttributeHeight (故事板中的“高度”)
  • relation = NSLayoutRelationLessThanOrEqual (童话板中的“小于或等于”)
  • 常量=你想要的线头高度

请参见故事板集成:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28438016

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档