I use iOS 6 , so attribute strings should be easy to use, right? Well ... not so much.
What I want to do:
Using a custom subclass of UIButton (it doesn't do anything custom for titleLabel ), I would like to have a multi-line related header that:
- All caps (I understand that not part of the attributes) in the first line
- Bold in the first line
- Underlined in the first line
- "Normal" weight in the second row
- No underline on the second line
- Centered on both lines
I was able to get No. 1 to 5 (at least I thought I did, but the current testing leads to multi-line text errors), but when I tried to do something (anything!) So that the text was centered, mine The application continues to crash. When I try to get all 6 elements working (using various methods), I get the following crash / error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSAttributedString invalid for autoresizing, it must have a single spanning paragraph style (or none) with a non-wrapping lineBreakMode.'
Based on what I tried, it seems that I can have one of the following options, but not both:
- Multi-line, centered label
- Attributed Label
I can live with one or the other if I have to , but I cannot believe that I cannot have what seems like a pretty simple concept.
Can someone please tell me what I'm wrong about?
Here's the last iteration of the code I'm trying:
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; [style setAlignment:NSTextAlignmentCenter]; [style setLineBreakMode:NSLineBreakByWordWrapping]; UIFont *font1 = [UIFont fontWithName:@"HelveticaNeue-Medium" size:20.0f]; UIFont *font2 = [UIFont fontWithName:@"HelveticaNeue-Light" size:20.0f]; NSDictionary *dict1 = @{NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle), NSFontAttributeName:font1}; NSDictionary *dict2 = @{NSUnderlineStyleAttributeName:@(NSUnderlineStyleNone), NSFontAttributeName:font2}; NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init]; [attString appendAttributedString:[[NSAttributedString alloc] initWithString:@"LINE 1\n" attributes:dict1]]; [attString appendAttributedString:[[NSAttributedString alloc] initWithString:@"line 2" attributes:dict2]]; [[self buttonToStyle] setAttributedTitle:attString forState:UIControlStateNormal]; [[[self buttonToStyle] titleLabel] setNumberOfLines:0]; [[[self buttonToStyle] titleLabel] setLineBreakMode:NSLineBreakByWordWrapping];
ios uibutton nsattributedstring
mbm29414 Jul 19 '13 at 21:50 2013-07-19 21:50
source share