ios 设置文本后通过自动布局获取 UITextView 动态高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27060080/
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-08-31 03:45:08  来源:igfitidea点击:

Get UITextView dynamic height with auto layout after setting text

iosobjective-cautolayoutuitextview

提问by Piero

I have a UITextView not scrollable with auto layout set by interface builder, and the text increase or decrease dynamically with no problem, but i want know what is the new UITextView height after setting text, i'm trying to do this:

我有一个 UITextView 不能通过界面构建​​器设置的自动布局滚动,并且文本动态增加或减少没有问题,但我想知道设置文本后新的 UITextView 高度是多少,我正在尝试这样做:

NSLog(@"text before: %.2f",self.myText.frame.size.height);
[self.myText setText:self.string];
NSLog(@"text after: %.2f",self.myText.frame.size.height);

this is the result:

这是结果:

text before: 47.50
text after: 47.50

the text is increased in the view when i run it, but the size is the same, how i can get the real height after setting text?

运行时文本在视图中增加,但大小相同,设置文本后如何获得实际高度?

采纳答案by Guillaume Algis

This should work:

这应该有效:

NSLog(@"text before: %.2f",self.myText.frame.size.height);
[self.myText setText:self.string];
[self.myText layoutIfNeeded]; // <--- Add this
NSLog(@"text after: %.2f",self.myText.frame.size.height);

Here's an example implementation on my Github: https://github.com/guillaume-algis/SO-27060338

这是我的 Github 上的示例实现:https: //github.com/guillaume-algis/SO-27060338

回答by Pavel Gurov

All you have to do is:

您所要做的就是:

  • set up all your constraints except the height oneAND
  • set textView's scrollEnabledproperty to NO
  • 设置除高度一之外所有约束
  • textViewscrollEnabled属性设置为NO

The last part is what does the trick.

最后一部分是诀窍。

Your text view will size automatically depending on its text value.

您的文本视图将根据其文本值自动调整大小。

回答by Michael Revlis

If you prefer to do it all by auto layout:

如果您更喜欢通过自动布局来完成这一切:

In Size Inspector:

在尺寸检查器中:

  1. Set Content Compression Resistance Priority Verticalto 1000.

  2. Lower the priority of constraint height for your UITextView. Just make it less than 1000.

  1. Content Compression Resistance Priority Vertical 设置为 1000。

  2. 降低 UITextView 的约束高度的优先级。让它小于1000。

enter image description here

在此处输入图片说明

In Attributes Inspector:

在属性检查器中:

  1. Uncheck Scrolling Enabled.
  1. 取消选中滚动已启用

回答by Parul Garg

I have used the code given on following link AutoLayout with Dynamic UITextView heightand it worked for me :)

我使用了以下链接AutoLayout 和动态 UITextView 高度上给出的代码,它对我有用:)

回答by Marco Martignone

Swift 3.0

斯威夫特 3.0

textView.isScrollEnabled = false

This allow AutoLayout to do its job.

这允许 AutoLayout 完成它的工作。

回答by Alok

Use below code:

使用以下代码:

Objective-C Code

Objective-C 代码

[textView setScrollEnabled:NO];

Swift Code

SWIFT代码

textView.isScrollEnabled = false

回答by merocode

Just after changing the text call

更改文字电话后

[self.myText sizeToFit];

回答by Hlung

Unlike UILabel, UITextView's has no intrinsic size property. So how I did it was set up the UITextView's height constraint, hook it via IBOutlet, and change its value in textViewDidChangeor when text changes.

与 UILabel 不同,UITextView 没有固有的大小属性。因此,我是如何设置UITextView的高度约束的,通过 IBOutlet 将其挂钩,并textViewDidChange在文本更改时或更改其值时更改它的值。

@IBOutlet weak var textViewHeight: NSLayoutConstraint!

func textViewDidChange(textView: UITextView) {

    // dynamic height adjustments

    var height = ceil(textView.contentSize.height) // ceil to avoid decimal

    if height != textViewHeight.constant { // set when height changed
        textViewHeight.constant = height
        textView.setContentOffset(CGPointZero, animated: false) // scroll to top to avoid "wrong contentOffset" artefact when line count changes
    }
}

回答by Hardik Kardani

- (void)textViewDidChange:(UITextView *)textView
{
    UIFont *myFont = [UIFont systemFontOfSize:14];
    CGSize size =   [self sizeOfText:textView.text widthOfTextView:TextviewWidth withFont:myFont];
    NSLog(@"Height : %f", size.height);
}

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

回答by Zain Ullah Muhammad

select textview and uncheck "Scrolling enabled"

选择 textview 并取消选中“启用滚动”

select textview from top menu "Editor > size to fit content"

从顶部菜单“编辑器 > 适合内容的大小”中选择 textview

select the view below it, set its top constraintswith the textview bottomto whatever margin you want, then go to "Size Inspector",double click or edit the constraint you just added, and set the "Relation" to "Greater than or Equal"

选择它下面的视图,将其顶部约束textview 底部设置为您想要的任何边距,然后转到“大小检查器”,双击或编辑您刚刚添加的约束,并将“关系”设置为“大于或等于”