开发者

How to make a UITextView scroll horizontally as the user types?

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 i开发者_Go百科t 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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜