如何使UITextView在用户键入时水平滚动? [英] How to make a UITextView scroll horizontally as the user types?

查看:68
本文介绍了如何使UITextView在用户键入时水平滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这看起来应该很简单,但是我不知道该怎么做。

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

我已经在界面生成器中创建了一个UITextView。它不位于表格单元格之类的东西中。

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

我想做的是,当用户使用UITextView时,将其自身向左滚动

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.

我试图在viewDidLoad和viewDidAppear中调整UITextView的内容大小,但这并没有会有所作为。

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

我也玩过IB中的插入设置,但这并不影响可滚动大小,只在显示它的地方显示。

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

有人有什么想法吗?

推荐答案

好吧,看起来好像可以完全按照我的意愿完成,但是我想出了一个办法让我到达那里。

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.

假设我希望可滚动宽度为600,可见宽度为250。

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

第一步是使UITextView宽到要滚动的区域。这样,您将在contentSize中输入的内容,在这种情况下为600。

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.

然后,将右插图设置为实际宽度和所需宽度之间的差。在这种情况下为350。

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

这种方式将光标限制在您需要的宽度上,但是在所需宽度的右​​边有可见的文本,毕竟,UITextView IS 600像素宽。

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.

在UITextView应该结束的右侧获取用户界面的图像。将其作为UIImage视图插入NIB,然后放回原处,并确保它位于UITextView的顶部。

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.

显示视图时,切口位于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.

这篇关于如何使UITextView在用户键入时水平滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆