iphone UitextView 文本被截断

发布于 2024-08-06 15:56:52 字数 1420 浏览 0 评论 0原文

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

I have a Uitextview that is populated with a text file. I have a control on the page that allows the user to enable and disable paging. I am having a problem where the top and/or bottom line of the text is sometimes "split in half" so you can only see the top or bottom half of the line. I believe the code below should fix it. The code gets the line height of the text and the frame height, figures out the number of lines that are visible on the screen, and creates a new frame height so it will fit. While the frame does get resized, it still "cuts off" the top and/or bottom line. Anyone one have any suggestions? Is my math wrong?

Thanks!!!

 - (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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

公布 2024-08-13 15:56:52

UIScrollView(UITextView 是 UIScrollView)似乎在两侧都使用固定的 8 像素插图。这似乎与对齐或字体大小无关。

所以,我认为你在这里缺少的是 16.0 的软糖系数 - 请参阅 这个问题。

UIScrollView (and UITextView is a UIScrollView) seems to use a fixed 8-pixel inset on both sides. This seems to be independent of alignment or font size.

So, I think what you're missing here is the fudge factor of 16.0 - see this question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文