Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent word wrap with UITextView

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

like image 738
tech74 Avatar asked Feb 04 '10 16:02

tech74


2 Answers

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 ...

like image 157
Pathogen Avatar answered Oct 12 '22 12:10

Pathogen


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.

like image 37
David H Avatar answered Oct 12 '22 11:10

David H