Question

I am trying to use NSAttributed String to display in the UILabel, and I am using word wrap as line break mode. But when I run the app, it throws exception "NSAttributedString invalid for autoresizing, it must have a single spanning paragraph style (or none) with a non-wrapping lineBreakMode."

Can someone suggest me how to resolve this?

Was it helpful?

Solution 3

You are using the NSMutableAttributedString in wrong way I assume, it should be used as under,

NSMutableDictionary *a = [[NSMutableDictionary alloc] init];
[a setObject:[NSNumber numberWithInt:1] forKey:NSStrikethroughStyleAttributeName]; 
NSMutableAttributedString *b = [[NSMutableAttributedString alloc] initWithString:@"abcd" attributes:a]; 
self.label.attributedText = b; 

after trying the above code, you should get the UILabel output like I have showed in image enter image description here

but if you want to set the attribute separately then you should do like this

[b addAttribute:ATTRIBUTENAME value:VALUE range:RANGE]; 

for example setting attribute for background color

[str addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(2,4)];

OTHER TIPS

To fix, set:

[yourLabel setAdjustsFontSizeToFitWidth:NO];

just make sure you don't override this later on in your code ;)

For anyone else who see this question. What helped me was disabling the resize of the UILabel's text. On my code I had this:

[myLabel setAdjustsLetterSpacingToFitWidth:YES];
[myLabel setAdjustsFontSizeToFitWidth:YES];
[myLabel setMinimumScaleFactor:15.0/myLabel.font.pointSize];

Removed it and started to work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top