我有一个用文本文件填充的 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;
}