iosobjective-cxcode4.6nsattributedstring

NSAttributeString Wrapping issue


I am trying to set attribute text to a label. The attributes seems to be a working the font as well as the color.

Only issue I am facing is the wrapping of lines. The Size of the UILabel is (200,300) with numberofLines=0. So with this it should wrap the lines, but it is not happening so.

   NSMutableString *title=[[NSMutableString alloc] init];
    NSRange range1;
    NSRange range2;
    NSRange range3;



        NSString *str1=@"ABCD EFGHI klm";
        [title appendString:str1];
        range1=NSMakeRange(0, str1.length);


        NSString *str2=@"PQRSSSS ";
        [title appendString:str2];
        range2=NSMakeRange(range1.length, str2.length);


        NSString *str3=@"1235 2347 989034 023490234 90";
        [title appendString:str3];
        range3=NSMakeRange(range2.location+range2.length, str3.length);


    NSMutableAttributedString *attributeText=[[NSMutableAttributedString alloc] initWithString:title];
    [attributeText setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:color1,NSForegroundColorAttributeName,[self getStlylishItalicFont:13.0] ,NSFontAttributeName,nil] range:range1];
    [attributeText setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:color2,NSForegroundColorAttributeName,[self getStylishFont:13.0] ,NSFontAttributeName,nil] range:range2];
    [attributeText setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:color3,NSForegroundColorAttributeName,[self getStylishBoldFont:13.0] ,NSFontAttributeName,nil] range:range3];

    self.myTextLabel.attributedText=attributeText;

UILabel is displayed like this, even though the height is 300.

ABCD EFGHI klm PQRSSSS 1235 234 ...


Solution

  • Make sure you set your UILabel's line break mode attribute to the one you desired like so:

    UILabel.lineBreakMode = NSLineBreakByWordWrapping;
    

    Or if you are using Interface Builder, you can do it there.