5

using Iphone sdk 3.1.2

I have a UITextView control with vertical and horizontal scrolling enabled. I want every line of text to display without wrapping so that the user can scroll horizontally to see it. The issue I have is that even with horizontal scroll enabled, the text wraps at the width of the iphone screen i.e.320 pixels.

How can I prevent wrapping?

Thanks

TechZen
  • 64,370
  • 15
  • 118
  • 145
tech74
  • 1,355
  • 3
  • 21
  • 35

7 Answers7

6

Use sizeWithFont: from the NSString UIKit additions to get each line's width, then set your UITextView's contentSize property to be slightly wider than the width of the longest line.

I had a problem trying this strategy, that arises when assigning a string to a UITextView's text property. On assignment, UITextView will set its contentSize.width to match the width of the view, and it will insert newlines into the assigned text string to wrap it to that width. You can set contentSize.width to the length of your longest line after assigning uitextview.text, and this gives the extra space in the scroll view, but the newlines stop the text from filling the new space.

If you set contentsize.width before loading your text into the view, the contentSize just gets reset when you do load your text.

I tried increasing the text view's frame size before loading, this works to let your long lines run on past the edge of the screen. But, you can't scroll, and the moment you reduce the frame size the newline mangling of your text and the readjustment of contentSize occurs again. The same occurs if any text is entered at all in the view.

Well, I'm convinced that uitextviews are simply not capable of or intended for horizontal scrolling at this point. I don't see how embedding a text view inside another scroll view could alleviate this either. Just have to do it in HTML ...

Pathogen
  • 845
  • 11
  • 16
3

I encountered similar problems. Here's my solution with Auto Layout.

  1. Put your UITextView in a scrollView. Stick the text view's borders to scroll view's borders.

  2. Use two IBOutlet for the UITextView's width and height. I call them "heightConstraint" and "widthConstraint"

  3. Use the following code in your view controller:

    - (void)viewWillLayoutSubviews {
        CGSize fit = [self.textView sizeThatFits:CGSizeMake(10000, 10000)];
        self.widthConstraint.constant = fit.width;
        self.heightConstraint.constant = fit.height;
    }
    

I figure out that you can just ask UITextView to calculate the size that fits, instead of calculating the length of each line by yourself

Harper
  • 1,794
  • 14
  • 31
3

The solution is to turn off all the scroll options on the UITextView itself, then embed it in another UIScrollView. You can get actual code by searching on this term "DualScrollTextView" in google.

David H
  • 40,852
  • 12
  • 92
  • 138
1

I think you're stuck with putting a UITextfield in a scrollview.

TechZen
  • 64,370
  • 15
  • 118
  • 145
0

Woot I got it.

I set the UITextView width to 500 on the .xib. On my code, I set the myUITextView.text = @"some huge string with many lines";

Then I set myUITextView.contentSize = CGSizeMake(600, myUITextView.contentSize.height);

Of course you can use whatever widths you want; just make sure the latter is bigger.

Carlos
  • 73
  • 7
0

Took me forever to figure out that my data had a \n in it. So it wasn't wrapping after all! Just in case this happens to you:

self.titleText.text = viewModel.title.replacingOccurrences(of: "\n", with: "");
user230910
  • 2,353
  • 2
  • 28
  • 50
0
textView.textContainer.lineBreakMode = .byTruncatingTail
textView.textContainer.maximumNumberOfLines = 1
Vadoff
  • 9,219
  • 6
  • 43
  • 39