Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a UITextView that automatically wraps lines of text?

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];
like image 473
Greg Avatar asked Jul 22 '11 01:07

Greg


1 Answers

Available since iOS 7:

textView.textContainer.lineBreakMode = NSLineBreakByWordWrapping;
like image 172
Vladimir Grigorov Avatar answered Oct 02 '22 15:10

Vladimir Grigorov