无法动态正确获取 uitextview 高度

发布于 2024-10-31 20:11:34 字数 577 浏览 1 评论 0原文

我在获取 uitextview 的正确大小时遇到​​问题。在我的应用程序中,我从 webservice 获取数据,并根据 webservice 的数据设置 uitextview 的高度。但是只要前 35 个字母(我的 textview 的框架)中有空格是 (20,0,255,2000))。那么计算出的 hjeight 总是比正确的高度少一行,甚至 uitext 视图也开始在新行中的空格之后打印该行(可能是单词很大,这就是为什么??)。

所以我得到了错误的文本视图高度。

文本视图看起来像这样:

firstword 空间空间空间空间 空间空间空间空间 空间空间空间空间 空间空间空间空间 spacespacesspacesspaces

当我输入没有空格的文本时,它会给我正确的高度,假设数据是“tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt” 然后在上面的场景中 Cgsize 将为我生成正确的结果。

在这里我没有使用 UILabel 是因为我想要链接检测,这就是原因。 如果有人有任何其他建议,我们非常欢迎。

问候 姆鲁根

I am having issue in getting the correct size of uitextview.In my application i am fetching the data from webservice and i set the height of uitextview as per the data from webservice .But whenever there is space in first 35 letters(frame of my textview is (20,0,255,2000)). then the hjeight calculated is always one line less then correct height and even the uitext view starts to print the line after space in the new line (may be word is big thats why ??).

So i am getting the wrong text view height.

The text view apears somwethibng like this :

firstword
spacesspacesspacesspaces
spacesspacesspacesspaces
spacesspacesspacesspaces
spacesspacesspacesspaces
spacesspacesspacesspaces

When i enter text which is say without spaces it gives me correct height suppose say the data is "ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt"
Then in the above scenerio Cgsize will generate proper result for me .

Here i am not using UILabel is because i want link detection thats why .
If anyone has anyother suggestions it is highly welcomed.

Regards
Mrugen

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

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

发布评论

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

评论(6

作业与我同在 2024-11-07 20:11:34

使用 NSString 的 sizeWithFont 方法并不真正适用于文本视图。它非常适合 UILabel,但文本视图似乎在文本周围有额外的填充。如果你使用这种方法,你最终得到的尺寸会有点短。

UITextView 是 UIScrollView 的子类。设置 textview 的 text 属性后,其 contentSize 属性会自动更新以适合文本。因此,如果您想设置文本视图的高度以适合其文本,只需执行以下操作:

textView.text    = @"some text";
CGRect rect      = textView.frame;
rect.size.height = textView.contentSize.height;
textView.frame   = rect;

这将设置您的 textView 的高度,使其包含文本。

Using NSString's sizeWithFont methods doesn't really work for a text view. It works great for a UILabel, but text views seem to have extra padding around the text. If you use that method, you end up with a size that's a little short.

UITextView is a subclass of UIScrollView. After you set the textview's text property, its contentSize property is automatically updated to fit the text. So, if you want to set the text view's height to fit its text just do:

textView.text    = @"some text";
CGRect rect      = textView.frame;
rect.size.height = textView.contentSize.height;
textView.frame   = rect;

That will set your textView's hight so it contains the text.

夏日浅笑〃 2024-11-07 20:11:34

您可以使用此函数测量文本的高度,并根据返回的高度设置框架。

   -(CGSize)sizeOfText:(NSString *)textToMesure widthOfTextView:(CGFloat)width withFont:(UIFont*)font
    {
        CGSize ts = [textToMesure sizeWithFont:font constrainedToSize:CGSizeMake(width-20.0, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap];
        return ts; 
    }

你这样使用它:

 CGSize size =   [self sizeOfText:@"the string you want to present" widthOfTextView:255 withFont:fontYouAreUsing];

然后将它传递给文本视图:

   textView.frame = CGRectMake(20,0,255,size.height);

祝你好运

沙尼

You can measure the height of the text with this function and set your frame according to the returned height.

   -(CGSize)sizeOfText:(NSString *)textToMesure widthOfTextView:(CGFloat)width withFont:(UIFont*)font
    {
        CGSize ts = [textToMesure sizeWithFont:font constrainedToSize:CGSizeMake(width-20.0, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap];
        return ts; 
    }

you use it like that:

 CGSize size =   [self sizeOfText:@"the string you want to present" widthOfTextView:255 withFont:fontYouAreUsing];

And then you pass it to the text view:

   textView.frame = CGRectMake(20,0,255,size.height);

Good luck

shani

温柔戏命师 2024-11-07 20:11:34

从 iOS7 开始,您现在应该使用:

CGRect textRect = [textToMesure boundingRectWithSize:CGSizeMake(width, FLT_MAX)
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:@{NSFontAttributeName:font}
                                     context:nil];

CGSize size = textRect.size;

As of iOS7 you should now use:

CGRect textRect = [textToMesure boundingRectWithSize:CGSizeMake(width, FLT_MAX)
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:@{NSFontAttributeName:font}
                                     context:nil];

CGSize size = textRect.size;
流殇 2024-11-07 20:11:34

你的问题的答案是
为 UITextView 创建一个实例,在我的例子中,我创建为 txtView,然后向其中添加文本并在控制台中打印出内容高度的高度。

NSLog(@"TextView高度为:%d",txtView.contentSize.height)

The answer for your question is
Create an instance for UITextView,In my case I have created as txtView and then add text to it and print out the height in content height in console.

NSLog(@"TextView Height is :%d",txtView.contentSize.height)

你是我的挚爱i 2024-11-07 20:11:34

获取任何 textField 的估计高度和宽度:(Swift 4)

它以 TextField 的文本作为参数。

private func estimateFrameForText(text: String) -> CGRect {

        let size = CGSize(width: 200.0, height: 1000)

        let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)

        return NSString(string: text).boundingRect(with: size, options: options, attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 17.0)], context: nil)
    }

Get the estimated Height and Width of any textField: (Swift 4)

It take the text of TextField as argument.

private func estimateFrameForText(text: String) -> CGRect {

        let size = CGSize(width: 200.0, height: 1000)

        let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)

        return NSString(string: text).boundingRect(with: size, options: options, attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 17.0)], context: nil)
    }
岁月流歌 2024-11-07 20:11:34

在 Swift 4 中尝试过,

textView.intrinsicContentSize.height

Tried in Swift 4,

textView.intrinsicContentSize.height

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