我有一个用文本文件填充的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;
}
答案 0 :(得分:1)
UIScrollView(和UITextView是一个UIScrollView)似乎在两侧使用固定的8像素插入。这似乎与对齐或字体大小无关。
所以,我认为你在这里缺少的是16.0的软糖因子 - 见this question.