Weird thing occurs when using sizetofit of a UITextView in IOS7

牧云@^-^@ 提交于 2019-11-29 11:53:10

问题


When I test my app on IOS7 simulator. Sometimes I found it is weird when I using sizeToFit of a UITextView. The frame after sizeToFit seems right but the text can only show partly just like the photo below. (The gray area represents the UITextView new frame after sizeToFit, the whole sentence should be "which sparked a tense relationship between the two.")

The UITextView text is set via attributedText. It seems the problem occurs with some sentences only and is OK for most sentences.

I met this problem several times and can not solve it yet. Any help will be appreciated. Thanks.

Update:

Finally I solve the problem in an ugly way. I reset the text of the textView.

NSString *text = textView.text;
textView.text = @"";
textView.text = text;

Now it can show the whole content after sizeToFit. I think it seems like a IOS 7's bug.


回答1:


I had the same problem, it took me a while to figure it out, you simply need to resize the text container

[textview sizeToFit];
[textview.textContainer setSize:textview.frame.size];



回答2:


Noticed in IOS7 sizeToFit wasn't working also - perhaps the solution may help you too, needs the additional layoutIfNeeded

[UITEXTVIEW sizeToFit];
[UITEXTVIEW layoutIfNeeded];
[UITEXTVIEW setTextContainerInset:UIEdgeInsetsMake(0, 0, 0, 0)];



回答3:


Have you got "clip subviews" checked in interface builder?

Try to uncheck it or setting this property to "NO".




回答4:


Try this it worked for me.

[textView setTextContainerInset:UIEdgeInsetsMake(0, 0, 0, 0)];


来源:https://stackoverflow.com/questions/18928832/weird-thing-occurs-when-using-sizetofit-of-a-uitextview-in-ios7

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