类似于iPhone上的短信应用程序的文本输入框
我在尝试获取像短信应用程序中那样的输入框时遇到了麻烦。
任何人都可以提供一些关于如何构建其中之一并使其看起来不错的指导吗?我需要一个形状与 UITextfield 类似的输入框,但在打字时会垂直拉伸。
我假设我需要计算文本的宽度并在文本换行时垂直拉伸覆盖图像框架。我最接近的确实会拉伸,但当接近边界时光标会四处弹跳。
更新:
我每天都在为此工作一周,我几乎放弃了 UITextView。我可以让它正确拉伸,但是当退格时,Textview 高度在向上一行时收缩太多。当我继续退格时,它会自行纠正。例如,当我在第 4 行并退格到第 3 行时,它会显示此行为。然后当我继续退格时,它会进行纠正,直到我到达第 2 行的末尾。然后它会自行纠正,.... 等等。
我决定尝试对 UITextField 进行子类化,但我什至无法让它以我指定的帧大小显示。 150,150,150,150。
I am having a terrible time trying to get an input box like the one in the SMS app.
Can anyone offer some guidance on how to build one of these and make it look good? I need an input box that is shaped nicely like the UITextfield but will stretch vertically when typing.
I assume that I need to calculate width of the text and stretch the overlay image frame vertically when the text word wraps. The closest I have come does stretch but the cursor bounces all around when nearing the boundaries.
UPDATE:
I have worked on this everyday for a week and I have about given up on the UITextView. I can get it to stretch properly but when backspacing, the Textview height shrinks too much when going up a line. As I continue backspacing it corrects itself. For example, it displays this behavior when I am on line 4 and backspace up to line 3. Then as I continue backspacing, it corrects until I get to the end of line 2. Then it corrects itself,.... etc.
I decided to try to subclass UITextField but I can't even get it to display in the Frame size that I specify. 150,150,150,150.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你必须自己做这件事。幸运的是,
UITextField
可以在其文本发生更改时通知您使用UITextFieldTextDidChangeNotification
,幸运的是NSString
有方法(在 UIKit Additions 下)让你 使用boundingRectWithSize:options:attributes:context:
获取给定特定宽度的字符串的高度,幸运的是您可以调整UITextField的大小 自己使用
frame
。
You get to do this yourself. Luckily, the
UITextField
can notify you whenever its text changes usingUITextFieldTextDidChangeNotification
, and luckilyNSString
has methods (under the UIKit Additions) that let you get the height of the string given a certain width usingboundingRectWithSize:options:attributes:context:
, and luckily you can resize aUITextField
yourself usingframe
.我知道这已经过时了,但对于仍然关注这个答案的人来说,现在 Slack 提供了一个很好的控件,可以实现名为 SlackTextViewController。
I know this is old but for the people that still look at this answer there is now a nice control from Slack that does that called SlackTextViewController.
尝试聊天输入示例。它具有与短信应用程序类似的外观和功能。
Try Chat Input Sample. It has the similar look and functionality of SMS app.
Three20 项目有一个控件可以为您执行此操作。
来自 Three20 自述文件:
Three20 project has a control that should do this for you.
From the Three20 README: