我有 2 个 NSAttributedString
,我将它们附加在一起,然后将其设置为图中 UILabel 中的文本。如果第一个属性字符串超过 2 行,我希望截断它,以便第二个属性字符串仍然显示。我该怎么做?
最佳答案
我认为最简单的方法是创建两个单独的标签来显示此文本。第一个将显示粗体文本,第二个将显示其余文本。您必须为第一个属性字符串设置 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/