2009-09-29 73 views
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; 
} 

回答

1

的UIScrollView(和UITextView的是一個UIScrollView)似乎使用一個固定的8像素插入在兩側。這似乎與對齊或字體大小無關。

所以,我認爲你在這裏缺少的是16.0的模糊因子 - 見this question.