14

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];
afuzzyllama
  • 6,538
  • 5
  • 47
  • 64
Greg
  • 323
  • 1
  • 3
  • 9
  • 1
    TextViews automatically add text wrapping:http://developer.apple.com/library/ios/#documentation/uikit/reference/uitextview_class/Reference/UITextView.html Are you sure you don't mean textFields? – chandhooguy Dec 25 '12 at 17:17

3 Answers3

6

Available since iOS 7:

textView.textContainer.lineBreakMode = NSLineBreakByWordWrapping;
Vladimir Grigorov
  • 10,903
  • 8
  • 60
  • 70
2

UITextView and UITextField are different classes. UITextField is single line and UITextView is multiline. Create a UITextView rather than a UITextField programmatically and you have solved your problem I expect.

mxcl
  • 26,392
  • 12
  • 99
  • 98
  • 7
    It's pretty clear from his code that he is creating an instance of `UITextView`. – PengOne Jul 22 '11 at 02:02
  • 1
    The first line of his answer is "Im am creating an app that creates a `UITextField` programmatically", hence my answer. – mxcl Jul 22 '11 at 02:06
  • I notice you edited his answer. I give odds 2-1 that it was just a typo in his code and he was using UITextField rather than UITextView, I've seen it done three or four times, it's an easy mistake to make and fits the provided evidence. Also, notably, UITextField doesn't have a horizontalScrollBar which explains the crash above when he tried to disable it. At the very least, you should undo the minus one you gave my answer :P – mxcl Jul 25 '11 at 17:39
  • 1
    I did edit the 1 typo and left the other 6 references to `textView` as they stood. If you look at the discussion he and I had after my solution, it's clear that he does indeed mean `UITextView`. The problem he has with creating it programmatically has been noticed by other people as well. Confusing the two is a common mistake, you are correct, but not in this case. – PengOne Jul 25 '11 at 20:05
0

The entire point of UITextView is to allow multiple lines of text. From the documentation:

The UITextView class implements the behavior for a scrollable, multiline text region. The class supports the display of text using a custom font, color, and alignment and also supports text editing. You typically use a text view to display multiple lines of text, such as when displaying the body of a large text document.

Since UITextView inherits from UIScrollView, you may be able to force it to cooperate by disabling the horizontal scrolling. To do this, you want to set the contentSize, e.g.

CGSize scrollableSize = CGSizeMake(210, 480);
[textView setContentSize:scrollableSize];

This will fix the width to 210 and allow the height to scroll to 480. Since you are setting the textView to size (210,60), this should allow the vertical scrolling but not the horizontal scrolling.

PengOne
  • 48,188
  • 17
  • 130
  • 149
  • That could be the problem, but is not in my case. Even when the textview is the size of the entire screen, with room for many lines, the text does not automatically wrap. Any other ideas? – Greg Jul 22 '11 at 02:06
  • @Greg: I just looked around for a bit, and found 3 posts, between Jun 09 and Jan 11, that ask this question and get no resolution. – PengOne Jul 22 '11 at 02:10
  • That's concerning, I feel like this is a very simple problem. Many apps utilize UITextViews with wrapping text, and they cant all have been created with IFB. – Greg Jul 22 '11 at 02:15
  • @Greg: Agreed. Did you try disabling the horizontal scrolling? – PengOne Jul 22 '11 at 02:16
  • how do you go about doing that? – Greg Jul 22 '11 at 03:45
  • I tried [textView setScrollEnabled = NO]; It compiled but caused the app to crash, and I feel like this isn't really the problem. Am I perhaps not specifying the boundaries for the text? – Greg Jul 22 '11 at 03:52
  • @Greg: check out my edit about. This is the "correct" way to disable one direction of scrolling. I'm still shocked there isn't an easier way to do this. – PengOne Jul 22 '11 at 04:02
  • I've just tried that, and it builds before crashing, the output saying "terminating app due to uncaught exception 'NSInvalidArgumentException', reason unrecognized selector sent to instance. Have you been able to get this to work on your computer? This is surprisingly frustrating, I really appreciate you helping me. – Greg Jul 22 '11 at 04:10