如何创建一个自动换行文本的 UITextView?
我正在创建一个以编程方式创建 UITextView 的应用程序。问题是,当用户输入文本或应用程序设置文本时,单词会跨行传播并离开屏幕,而不是换行到下一行。当我使用界面生成器创建 UITextView 时,文本会自动换行,但以编程方式创建时不会。
代码:
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(100, 12, 210, 60)];
[[self view] addSubview:textView];
[textView becomeFirstResponder];
I am creating an app that creates a UITextView programmatically. The problem is, when the user types in text or the app sets the text, the words spread across one line and off the screen, rather than wrapping to the next line. When I create a UITextView with interface builder, the text wraps automatically, but not when it is created programmatically.
Code:
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(100, 12, 210, 60)];
[[self view] addSubview:textView];
[textView becomeFirstResponder];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
自 iOS 7 起可用:
Available since iOS 7:
UITextView
和UITextField
是不同的类。UITextField
是单行,UITextView
是多行。以编程方式创建UITextView
而不是UITextField
,并且您已经解决了我期望的问题。UITextView
andUITextField
are different classes.UITextField
is single line andUITextView
is multiline. Create aUITextView
rather than aUITextField
programmatically and you have solved your problem I expect.UITextView
的全部要点是允许多行文本。从文档中:由于
UITextView
继承自UIScrollView
,因此您可以通过禁用水平滚动来强制它配合。为此,您需要设置contentSize
,例如,这会将宽度固定为
210
并允许高度滚动到480
。由于您将textView
设置为(210,60)
大小,因此应该允许垂直滚动,但不允许水平滚动。The entire point of
UITextView
is to allow multiple lines of text. From the documentation:Since
UITextView
inherits fromUIScrollView
, you may be able to force it to cooperate by disabling the horizontal scrolling. To do this, you want to set thecontentSize
, e.g.This will fix the width to
210
and allow the height to scroll to480
. Since you are setting thetextView
to size(210,60)
, this should allow the vertical scrolling but not the horizontal scrolling.