views:

381

answers:

2

This seems like it should be simple but I can't work out how to do it.

I've created a UITextView in interface builder. It doesn't sit in a table cell or anything fancy like that.

What I'd like to do is have the UITextView scroll itself to the left when the user has typed their way all the way to the right margin.

At the moment it just does a word wrap, I understand the word wrap makes sense in most situations but I need it to scroll instead. What I'm after is the same behavior it exhibits vertically.

I've tried adjusting the content size of the UITextView in viewDidLoad and also in viewDidAppear and that doesn't make a difference.

I've also played with the inset settings in IB but that doesn't affect the scrollable size, just WHERE it's displayed.

Does anyone have any ideas?

A: 

If you only need a single line of text that scrolls, you would probably be better served by a UITextField. UITextViews are meant for multi-line text input, which is why word wrap is the default. You can't, as far as I know, override that behavior.

Alex
Actually, I need multiple lines.
oldbeamer
You could try setting/overriding the `contentSize` property, but if that doesn't work, you're probably out of luck.
Alex
A: 

Ok, it doesn't look like it can be done exactly how I wanted but I've come up with a hack to get me there.

Let's say I want a scrollable width of 600 and a visible width of 250.

The first step is to make the UITextView a wide as the area you want to be scrollable. So what you would have put into contentSize, in this case 600.

Then the right inset is set to the difference between the actual width and the width you wanted. In this case 350.

This way cursor is restrained to the width you need BUT there is text visible to the right of your desired width, after all, the UITextView IS 600 pixels wide.

Now here's where the really hackish bit comes in, don't read on if you're sensitive or have a weak stomach.

Get an image of the user interface to the right where UITextView should end. Insert it into the NIB as an UIImage view and put it back in it's place, making sure that it's on top of the UITextView.

When the view is displayed, the cutout sits on top of the UITextView and hides the text overrun.

I'm not proud, but it works.

oldbeamer