문제

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?

도움이 되었습니까?

해결책 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)];

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top