ios - UILabel 换行截尾

标签 ios objective-c

我正在使用 Storyboard 来设置 UILabel 的行数 (4) 和换行符(截尾)。

This is what i have right now:

enter image description here

我的问题是如何截断/触发截断?

This is what i want to achieve:

enter image description here

更新:

只是为了让我的问题更清楚。
我不想像默认 UILabel 的行为那样剪切 UILabel 中的实际字符串。

使用 NSLog(@"%@", myLabel.text); 时第一张图片生成的日志是分配给它的完整字符串,这就是我试图实现的行为.

在我的例子中是:

A little girl was talking to her teacher about whales. The teacher said it was physically impossible for a whale to swallow a human because even though it was a very large mammal.

---

Making my question clearer:

如何在不剪切实际的情况下截断/触发截断 NSString 分配给它?

或者可能会解决这个问题,这有可能吗?

最佳答案

您可以使用 TTTAttributedLabel图书馆

例如:

enter image description here

声明:

@property (strong, nonatomic) IBOutlet TTTAttributedLabel *lblTT;

带有 setAttributedTruncationToken 的示例代码:

NSDictionary *attr = @{NSForegroundColorAttributeName : [UIColor redColor]};

NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"...Continue Reading" attributes:attr];

[self.lblTT setAttributedTruncationToken:str];

希望对你有所帮助。

如果您不想使用现成的类(class),请检查此答案:How to add button to the end of text like Facebook's "Continue reading"?

关于ios - UILabel 换行截尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31420543/

相关文章:

ios - 如何在 Objective C 中的 UI 导航栏上显示通知计数

ios - 如何在不重新加载 Objective-C 行的情况下更改行中的特定元素?

objective-c - UICollectionView 的 indexPath 值

ios - UICollectionview insertItemsAtIndexPaths 停止滚动

iphone - 尝试设置分发配置文件时出现问题

ios - 为什么 iPhone 7 和 iPhone 7 Plus 屏幕截图会发送与 RGB 色彩空间图像不兼容的 alpha?

ios - if(NSOrderedAscending == result) 有人能解释一下吗

ios - 从自定义 uitableviewcell 中删除自定义子层

objective-c - 在哪里为 iOS 应用程序创建全局变量?

objective-c - 我的 NSBezierPath 在 NSCFTimer 中发生变化