简体   繁体   English

如何在Swift中根据用户类型调整UITextField的大小

[英]How to resize UITextField as user types in Swift

I'm trying to write a messaging application in Swift on IOS8. 我正在尝试在IOS8上的Swift中编写一个消息传递应用程序。 Essentially I'm trying to mimic what the texting app in IOS does when you reach the edge of the textfield--the height of the textfield increases, and a new line is made. 本质上,我试图模仿当您到达文本字段的边缘时IOS中的文本应用程序所做的事情-文本字段的高度增加,并换行。 Right now when I type, the words just scroll to the side. 现在,当我键入内容时,单词会滚动到侧面。 How can I prevent the side scrolling and instead resize the textfield? 如何防止侧面滚动,而是调整文本字段的大小?

I would definitely use UITextView combined with its textViewDidChange: delegate method. 我肯定会使用UITextView及其textViewDidChange:委托方法。 You can track when the inside text changes and update your textView width (or height) accordingly by measuring its content size from constraints you specify. 您可以通过根据指定的约束测量其内容大小来跟踪内部文本的更改时间并相应地更新textView的宽度(或高度)。

You can also reimplement drawRect: to add your rounded borders or customize properties of its layer (like cornerRadius ). 您还可以重新实现drawRect:以添加圆角边框或自定义其layer属性(例如cornerRadius )。

Placeholder can be also implemented by tracking text changes (whether by delegate methods or notifications). 占位符也可以通过跟踪文本更改来实现(无论是通过委托方法还是通过通知)。

I agree this isn't optimal, but at least you still have options to mimic UITextField behaviors. 我同意这不是最佳选择,但至少您仍然可以选择模仿UITextField行为的选项。

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

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