简体   繁体   English

设置UITextView的高度约束

[英]Set height constraint for UITextView

I'm fairly new to iOS development and am having a hard time sizing a UITextView to it's content. 我对iOS开发相当新,并且很难将UITextView调整为内容。 I've found several examples of calculating a strings render size and/or updating the frame size of a textview, however I can't seem to get those to work. 我已经找到了几个计算字符串渲染大小和/或更新textview的帧大小的例子,但是我似乎无法让它们起作用。

When I add a UITextView to my view it adds a Height constraint which I think is locking it to a specific height. 当我在视图中添加UITextView时,它会添加一个高度约束,我认为它将其锁定到特定高度。 How can I work around it, update the constraint via code, and/or get my UITextView to resize vertically to show my content? 我如何解决它,通过代码更新约束,和/或让我的UITextView垂直调整大小以显示我的内容?

您可以创建垂直大小约束的出口,然后调整约束的.constant属性以修改文本视图的高度。

A UITextView inherits from UIScrollView. UITextView继承自UIScrollView。 As part of it's logic, it changes the height of its contentSize property to allow the user to scroll vertically to any portion of the TextView's content. 作为其逻辑的一部分,它更改其contentSize属性的高度,以允许用户垂直滚动到TextView内容的任何部分。

I would recommend using the ContentSize property of your UITextView to figure out how "tall" your content really is. 我建议使用UITextView的ContentSize属性来确定你的内容真正“高”。 You can then set the frame of your UITextView accordingly. 然后,您可以相应地设置UITextView的框架。

OR: You could figure out the size of your text using the right method from the NSString+UIKitAdditions category. 或者:您可以使用NSString + UIKitAdditions类别中的正确方法计算出文本的大小。

If you don't want autolayout then disable it in the xib file. 如果您不想自动布局,请在xib文件中禁用它。

If you want to change the contraint and it's value this tutorial will help you to deal with autolayout. 如果你想改变约束并且它的价值,本教程将帮助你处理自动布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM