問題描述
我有一個 UITableViewCell 子類,其中包含一個 UITextView 滾動已關閉.我在表格視圖單元格 layoutSubviews 方法中這樣設置它的框架:
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):
框架是基于文本的正確大小(在本例中為 1019 磅),但文本在文本視圖底部之前停止.我還看到文本在一行中被截斷(即最后一個可見文本行的文本被水平截斷一半).有誰知道這里發生了什么?
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?
其他幾點:
- 文本視圖適用于我所有內容較短的表格視圖單元格.
- 如果我在上面顯示的情況下增加文本量,文本視圖高度會增加,但文本仍然會在同一位置被截斷.
推薦答案
根據this 和類似的答案,問題可能是正確的 contentSize 僅在 UITextView 添加到視圖后可用......在此之前它等于 frame.size"
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"
我建議您以不同的方式計算高度,例如 -sizeWithFont: 或 -sizeTofit
I'd suggest you to calculate the height in a different way, like -sizeWithFont: or -sizeTofit
這篇關于文本量大時UITextView文本被截斷的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!