ios - 截断 NSAttributedString 中的部分字符串

标签 ios objective-c cocoa-touch nsattributedstring

我有 2 个 NSAttributedString,我将它们附加在一起,然后将其设置为图中 UILabel 中的文本。如果第一个属性字符串超过 2 行,我希望截断它,以便第二个属性字符串仍然显示。我该怎么做?

enter image description here

最佳答案

我认为最简单的方法是创建两个单独的标签来显示此文本。第一个将显示粗体文本,第二个将显示其余文本。您必须为第一个属性字符串设置 NSLineBreakMode,如下例所示:

NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setLineBreakMode:NSLineBreakByWordWrapping];


NSDictionary *attributes = @{/*yourattributes */NSParagraphStyleAttributeName: style};
NSAttributedString = [[NSAttributedString alloc] initWithString:YOUR_TEXT attributes:attributes];

如果您将文本拆分为两个单独的标签,您甚至可以放弃使用 NSAttributedString。相反,您可以使用 UILabel 方法来自定义文本外观。

 label.font = font;
 label.textColor = [UIColor grayColor];

关于ios - 截断 NSAttributedString 中的部分字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22235218/

相关文章:

ios - 如何在 Swift 中终止(不是 segue)一个 View

ios - 每个单元格上的 UIRefreshControl(拉动做某事)

iOS ARC block 保留周期

iphone - 传递错误委托(delegate)

ios - 将最近保存的数据与 Core Data 一起使用

cocoa-touch - 在 iOS8 中使用 clipsToBounds = YES 时状态栏完全变黑。我该如何改变这个?

ios - JS 代码不在 iOS Web 应用程序模式下执行,但在 iOS Safari 中运行良好

Objective-C#导入整个目录

ios - NSMutableArray 索引变得非常高并且 removeObjectAtIndex 超出范围

ios - 在 Objective C 中调用同一个 View "N"次