UITextView 与底部对齐
我想知道如何设置 UITextView 以使里面的文本与底部对齐?就像,如果你开始打字,你总是会在最下面一行打字?
I wondered how to set the UITextView so that the text inside aligns to the bottom? Like, if you start typing, you will always type in the lowest row?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不相信有一种内置方法可以垂直对齐
UITextView
中的文本,但您应该能够使用的
类来完成此任务。有关更多详细信息,请参阅此博客文章:contentOffset
属性UITextViewhttp://imagineric.ericd.net/2011/03/10/ios-vertical-aligning-text-in-a-uitextview/
更新:自上述链接似乎已失效,我使用 Wayback Machine 检索 存档副本 并粘贴了下面文章的文本。
I don't believe there is a built-in way to vertically align the text in a
UITextView
, but you should be able to use thecontentOffset
property of theUITextView
class to accomplish this. See this blog post for more details:http://imagineric.ericd.net/2011/03/10/ios-vertical-aligning-text-in-a-uitextview/
Update: since the above link appears to be dead, I used the Wayback Machine to retrieve an archived copy and have pasted the text of the article below.
一种方法可能是使用多行 UILabel,然后使用如下方法:
http://texnological.blogspot.com/2011/08/how-to-do-vertical-alignment-for.html
感谢作者!
One method could be to use a multi-line UILabel then use a method like the following:
http://texnological.blogspot.com/2011/08/how-to-do-vertical-alignment-for.html
Credit to the author!
使用自动布局对我有用:
1)布局您的
UITextView
,以便高度足够大以容纳单行文本,并且底部固定在屏幕底部(或文本视图下方的某个位置) 。2) 在视图控制器中保留对文本视图高度的
NSLayoutConstraint
的引用。3) 将视图控制器设置为 UITextViewDelegate
4) 输入文本时调整文本视图的高度并强制布局。例如:
Using autolayout worked for me:
1) Layout your
UITextView
so that the height is large enough for a single line of text and the bottom is pinned to the bottom of the screen (or something underneath the text view).2) Keep a reference in your view controller to the
NSLayoutConstraint
for the text view height.3) Setup your view controller to be a
UITextViewDelegate
4) adjust the height of the text view when text is entered and force a layout. For example: