Reputation: 545
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 ...
Upvotes: 2
Views: 4275
Reputation: 1
along with setting:
self.myTextLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.myTextLabel.numberOfLines = 0;
You might also need to set your label's layout constraints to it's superview. I ran into this when I neglected to pin my label's trailing constraint to the my label's superview.
Upvotes: 0
Reputation: 3581
What you need is the NSParagraphStyle attribute :
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentLeft;
paragraphStyle.lineSpacing = 1;
//Now add this to your attributes dictionary for the key NSParagraphStyleAttributeName eg,
@{NSParagraphStyleAttributeName:paragraphStyle,...}
On an unrelated note you know its better to create dictionaries in the modern Objective-c format. Whenever I don't, my mentor get's angry. That would look something like this :
[attributeText setAttributes:@{NSForegroundColorAttributeName:color1, NSFontAttributeName:[self getStlylishItalicFont:13.0], NSParagraphStyleAttributeName:paragraphStyle, }];
//The trailing comma in the dictionary definition is not at typo it is important.
Upvotes: 5
Reputation: 2575
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.
Upvotes: 2
Reputation: 1414
I think so u are facing the problem with the uilabel increasing the height , If u need the multiple lines in label then u have to give the property of havin numberoflines=0; after that u have to resize the label frame according to the size of text u are giving to the label.
Please check the below code i may be useful to u,
NSString *someText = [NSString stringWithFormat:@"%@",[[arrFulldetails objectAtIndex:indexPath.row]valueForKey:@"MessageText"]];
CGSize constraintSize;
constraintSize.width = 300.0f;
constraintSize.height =100000;
CGSize stringSize =[someText sizeWithFont: [UIFont boldSystemFontOfSize: 17] constrainedToSize: constraintSize lineBreakMode: UILineBreakModeWordWrap];
CGRect rect ;
rect = CGRectMake(10, 150, 210, 20+stringSize.height);
Upvotes: 0
Reputation: 409
Why can't you set the numberoflines to 1.Because wrapping makes sense but number of lines 0 doesnt make sense.. and also you have to set proper frame for label.
Upvotes: 0