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

房东的猫 提交于 2019-12-07 08:02:44

问题


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!


回答1:


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



来源:https://stackoverflow.com/questions/13298748/using-attributedtext-nsattributedstring-on-a-uitextview-on-ios6-only-works-wit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!