xcode 无法动态正确获取 uitextview 高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5638998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:52:59  来源:igfitidea点击:

cant get the uitextview height dynamically correct

iphonexcodeuitextview

提问by mrugen

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 ??).

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

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 .

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

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

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

Regards Mrugen

问候姆鲁根

回答by CharlieMezak

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.

使用 NSString 的 sizeWithFont 方法实际上不适用于文本视图。它适用于 UILabel,但文本视图似乎在文本周围有额外的填充。如果您使用这种方法,您最终会得到一个有点短的尺寸。

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:

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

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.

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

回答by shannoga

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

沙尼

回答by Alberto Lopez

As of iOS7 you should now use:

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

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

CGSize size = textRect.size;

回答by Alberto Lopez

Get the estimated Heightand Widthof any textField: (Swift 4)

获取估计的HeightWidth任何textField:(Swift 4)

It take the text of TextField as argument.

它以 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)
    }

回答by Leon Mak

Tried in Swift 4,

在 Swift 4 中尝试过,

textView.intrinsicContentSize.height

textView.intrinsicContentSize.height

回答by vivek

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.

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

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

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