UITextView text cut off when there is a large amount of text

十年热恋 提交于 2019-12-19 08:54:11

问题


I have a UITableViewCell subclass that contains a UITextView where scrolling is turned off. I set its frame like this, in my table view cell layoutSubviews method:

    CGRect frame = self.bodyTextView.frame;
    CGSize size = self.bodyTextView.contentSize;
    frame.size = size;
    self.bodyTextView.frame = frame;

This has been working fine for some time, but I've noticed that in a case where I have a particularly large amount of text, the text is getting cut off. I've set the text view frame background color to orange so I could verify that the frame was being set correctly. Here is an example (I am only showing the bottom portion of the text view):

The frame is the correct size based on the text (in this case 1019 points), but the text stops before the bottom of the text view. I have also seen the text get cut off part way through a line, (ie the text of the last visible line of text is cut off half way through horizontally). Does anyone have an idea what is happening here?

A few other points:

  • The text views work well for all my table view cells with shorter content.
  • If I increase the amount of text in the case shown above, the text view height increases, but the text still gets cut off at the same place.

回答1:


According to this and similar answers, the problem could be that "the correct contentSize is only available after the UITextView has been added to the view ... Prior to that it is equal to frame.size"

I'd suggest you to calculate the height in a different way, like -sizeWithFont: or -sizeTofit




回答2:


Use this to get the textSize

// find the text width; so that btn width can be calculate
CGSize textSize = [@"YOUR TEXT" sizeWithFont:[UIFont fontWithName:@"Arial" size:20.0] 
                              constrainedToSize:CGSizeMake(320.0f, 99999.0f)
                                  lineBreakMode:UILineBreakModeWordWrap];

and then set height using this as :

CGRect frame = self.bodyTextView.frame;

frame.size = textSize;
self.bodyTextView.frame = frame;

Hope it helps you.




回答3:


I had the same issue and resolved it working on the textView padding (in swift5):

yourTextView.textContainerInset = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)

Hope it can help :)



来源:https://stackoverflow.com/questions/16836707/uitextview-text-cut-off-when-there-is-a-large-amount-of-text

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