Search code examples
iosuilabelnsattributedstring

lineBreakMode for multiline attributed string


I have a multiline label that will contain an attributed string. This is the code of the attributed string:

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]];

In my case the label is shown with ellipse "..." in all the lines. how can i resolve this issue. I want that will be shown only in the last visible line.


Solution

  • The solution that i adopted is

    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 :)
    

    I know that it is not a clean solution but it resolve my issue until finding a clean one.