简体   繁体   中英

Using attributedText (NSAttributedString) on a UITextView on iOS6 only works with paragraph style or font but not both

I have been trying to use the attributedText property of UITextView to add adjustable line height to my custom UITextView. My code works fine in the simulator, but I am unable to make it work on an iPhone5. If I remove the font line the line height works but it the text reverts to a default smaller font. If I add the font, the font works but the paragraph styling is ignored. I have tried the code on a vanilla UITextView in a fresh app with the same behavior, which makes me think this is an iOS6 bug. Has anyone had any better luck?

I have also tried various UITextView replacements to add line height functionality, but nothing has worked out so far.

My code looks like this:

...
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.minimumLineHeight = lineHeight;
paragraphStyle.maximumLineHeight = lineHeight;

NSString *text = self.text;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];

[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, text.length)];
[attributedString addAttribute:NSFontAttributeName value:self.font range:NSMakeRange(0, text.length)];

self.attributedText = attributedString;

Thanks for any confirmation or suggestions anyone has!

I think this is indeed a bug. I've been struggling with it for the past few days and couldn't find any reasonable explanation.

Also, if you use IB to set up your UITextView, a few of those attributes won't be set as it should.

Anyway, I just filed a bug on that. http://openradar.appspot.com/radar?id=2278401

Matt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM