xcode 如何使用swift iOS(不使用UITextView)制作多行扩展UTextField或文本输入

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

How can I make a multi line expanding UTextField or text input using swift iOS (not using UITextView)

iosswiftxcodeuitextfield

提问by alionthego

Looking at the behavior of the Apple Messages app, it seems they are using a UITextField for the text input.

查看 Apple Messages 应用程序的行为,似乎他们正在使用 UITextField 进行文本输入。

The behavior of the placeholder with the cursor appearing at the beginning of the placeholder text (essentially overwriting it) is what I'm basing that assumption on. There is no placeholder for a UITextView that I'm aware of.

占位符和光标出现在占位符文本的开头(基本上是覆盖它)的行为是我基于该假设的。我所知道的 UITextView 没有占位符。

I know I can easily use a UITextView and add a placeholder with different color and code the placeholder disappearing when the user starts typing. But I was wondering if there is any way at all to program a UITextField that can have it's frame be fixed in width and expand in height wrapping text.

我知道我可以轻松地使用 UITextView 并添加具有不同颜色的占位符,并对占位符在用户开始输入时消失的代码进行编码。但我想知道是否有任何方法可以对 UITextField 进行编程,使其框架的宽度固定并扩展包装文本的高度。

回答by shraddha hattimare

You can not expand UITextFiled's height.

您不能扩展 UITextFiled 的高度。

But you can do this using UITextView ,what you have to do is

但是你可以使用 UITextView 来做到这一点,你需要做的是

Instead of UITextFiled , use UITextView and set ScrollEnabled to false , give UITextView top, bottom, leading and trailing constraint.

而不是 UITextFiled ,使用 UITextView 并将 ScrollEnabled 设置为 false ,给 UITextView 顶部、底部、前导和尾随约束。

Give height constraint to UITextView and set its content hugging priority at low.

为 UITextView 提供高度约束并将其内容拥抱优先级设置为低。

now your textview will expand according to text which are insert in it.

现在您的 textview 将根据插入其中的文本展开。

Yes, you have to add and manage placeholder logic manually

是的,您必须手动添加和管理占位符逻辑