1
我有一個Uitextview填充文本文件。我在頁面上有一個控件,允許用戶啓用和禁用分頁。我有一個問題,文本的頂部和/或底部有時會「分成兩半」,因此您只能看到該行的頂部或底部一半。我相信下面的代碼應該修復它。該代碼獲取文本的行高度和幀高度,計算出屏幕上可見的行數,並創建一個新的幀高度,以便適合它。在框架調整大小的同時,它仍然會「切斷」頂部和/或底部線條。任何人有任何建議?我的數學錯了嗎?iphone UitextView文本截斷
謝謝!
- (void)fitText
{
CGFloat maximumLabelHeight = 338;
CGFloat minimumLabelHeight = 280;
CGSize lineSize = [theUiTextView.text sizeWithFont:theUiTextView.font];
CGFloat lineSizeHeight = lineSize.height;
CGFloat theNumberOfLinesThatShow = theUiTextView.frame.size.height/lineSize.height;
//adjust the label the the new height
theNumberOfLinesThatShow = round(theNumberOfLinesThatShow);
CGFloat theNewHeight = lineSizeHeight * theNumberOfLinesThatShow;
if (theNewHeight > maximumLabelHeight)
{
theNumberOfLinesThatShow = theNumberOfLinesThatShow - 1;
theNewHeight = lineSizeHeight * theNumberOfLinesThatShow;
}
if (theNewHeight < minimumLabelHeight)
{
theNumberOfLinesThatShow = theNumberOfLinesThatShow + 1;
theNewHeight = lineSizeHeight * theNumberOfLinesThatShow;
}
//adjust the label the the new height.
CGRect newFrame = theUiTextView.frame;
newFrame.size.height = theNewHeight;
theUiTextView.frame = newFrame;
}