多行属性字符串的lineBreakMode

时间:2015-02-24 15:24:32

标签: ios uilabel nsattributedstring

我有一个包含属性字符串的多行标签。这是属性字符串的代码:

NSString * titleString = @"Title";
NSString * informationString = self.myObject.content;
NSString * allString = [NSString stringWithFormat:@"%@\n\n%@",titleString,informationString];
NSMutableAttributedString* attributedMessage =  [[NSMutableAttributedString alloc]initWithString:allString];
NSMutableParagraphStyle* style=[[NSMutableParagraphStyle alloc]init];
style.alignment = NSTextAlignmentLeft;


NSDictionary *attributesTitle = [NSDictionary dictionaryWithObjectsAndKeys:style,NSParagraphStyleAttributeName,[UIColor blackColor],NSForegroundColorAttributeName,[UIColor clearColor],NSBackgroundColorAttributeName,Font(@"OpenSans-Semibold", 17+(int)((self.view.bounds.size.width-290.)/15.)),NSFontAttributeName,nil];

NSMutableParagraphStyle* styleText=[[NSMutableParagraphStyle alloc]init];
styleText.alignment = NSTextAlignmentLeft;
styleText.lineBreakMode = NSLineBreakByTruncatingTail;

NSDictionary *attributesInformation = [NSDictionary dictionaryWithObjectsAndKeys:styleText,NSParagraphStyleAttributeName,[UIColor colorWithRed:91./255 green:91./255 blue:91./255 alpha:1.],NSForegroundColorAttributeName,[UIColor clearColor],NSBackgroundColorAttributeName,[UIFont fontWithName:@"Roboto-Light" size:13.+(int)((self.view.bounds.size.width-290.)/15.)],NSFontAttributeName,nil];

[attributedMessage setAttributes:attributesTitle range:[allString rangeOfString:titleString]];

[attributedMessage setAttributes:attributesInformation range:[allString rangeOfString:informationString]];

在我的情况下,标签在所有行中都显示为椭圆“...”。我该如何解决这个问题。我希望它只会显示在最后一行。

1 个答案:

答案 0 :(得分:0)

我采用的解决方案是

while (newHeight>=heightOfLabel){
remove some letter from the end of the string
calculate new height
}
replace the last three charachter with "..."
and it is done :)

我知道这不是一个干净的解决方案,但它解决了我的问题,直到找到一个干净的解决方案。