[英]“Manually” handling text and cursor position of UITextView breaks when there are multiple lines
I have a narrow UITextView
(about 1 line in height) to which I only add text programmatically, setting the text
property of it as I click buttons. 我有一个狭窄的
UITextView
(大约1行高),我只能以编程方式添加文本,并在单击按钮时设置它的text
属性。 I also handle the cursor position manually, setting the selectedRange
as the text changes. 我还手动处理光标位置,随着文本的更改设置
selectedRange
。 All works great as long as I have only one line of text. 只要我只有一行文字,所有作品都很棒。
When I have two or more lines of text and try to insert text at the first line, the text is inserted correctly and the cursor position is still at the right place but the UITextView
scrolls to the bottom. 当我有两行或更多行文本并尝试在第一行插入文本时,该文本将正确插入,并且光标位置仍在正确的位置,但是
UITextView
滚动到底部。 When I then add another piece of text at the top it scrolls up to the "correct" position. 然后,当我在顶部添加另一条文本时,它将向上滚动到“正确”位置。 This pattern then repeats for every entered piece of text at a line other than the last, making the UITextView scroll up and down for every button pressed.
然后,该模式针对除最后一行以外的每一行输入的每个文本重复执行此操作,从而使UITextView对于每个按下的按钮都向上和向下滚动。
I also tried calling UITextView scrollRangeToVisible:
passing the selectedRange property as argument after setting the new text. 我还尝试调用
UITextView scrollRangeToVisible:
在设置新文本后将selectedRange属性作为参数传递。 That didn't work either. 那也不起作用。
I finally tried setting selectedRange
after a 0.5 s delay after I set the text
. 设置
text
后,我最终尝试在0.5 s的延迟后设置selectedRange
。 Then it works as it should, but only after the UITextView
has first been scrolled down to the bottom for 0.5 s. 然后,它将按
UITextView
工作,但UITextView
是首先将UITextView
向下滚动到底部持续0.5 s。 This seems to indicate that the setText
method of UITextView is asynchronous in some way, and completes after I have already set selectedRange
or called scrollRangeToVisible
, and readjusts the UITextView to what it believes is the desired. 这似乎表明UITextView的
setText
方法在某种程度上是异步的,并且在我已经设置了selectedRange
或称为scrollRangeToVisible
,并将UITextView重新调整为它认为是所需的。
Can anyone tell me what is going on, and how I can get around the problem. 谁能告诉我怎么回事,以及如何解决这个问题。
Thanks! 谢谢!
The auto scroll behavior of UITextField
s can be annoying and difficult to harness at times. UITextField
的自动滚动行为可能很烦人,有时很难利用。
Do you need the user to manually edit the text? 您需要用户手动编辑文本吗? If not, use a multiline
UILabel
. 如果不是,请使用多行
UILabel
。
If yes, also use a multiline UILabel
and exchange it on the fly against a UITextView
once the user taps. 如果是,则还使用多行
UILabel
并在用户点击后立即将其与UITextView
交换。 Change it back on didEndEditing
. 将其更改回
didEndEditing
。 This techniques has worked well for me in table views. 在表视图中,这种技术对我来说效果很好。
I solved the problem! 我解决了问题! When starting in the maze of creating a custom input view, I began implementing
UITextInput
and I found that the UITextInput
protocol declares many interesting methods. 在创建自定义输入视图的迷宫中开始时,我开始实现
UITextInput
并且发现UITextInput
协议声明了许多有趣的方法。 And what is awesome is that UITextView
implements that protocol. 令人敬畏的是
UITextView
实现了该协议。 So even though it's not in the immediate UITextView
API reference, you can call all UITextInput
methods on your UITextView
. 因此,即使它不在直接的
UITextView
API参考中,也可以在UITextView
上调用所有UITextInput
方法。 For instance insertText:
(from UIKeyInput
protocol) and replaceRange:withText:
. 例如
insertText:
来自UIKeyInput
协议)和replaceRange:withText:
They made it much more flexible to programmatically work with a UITextView
. 他们使以编程方式使用
UITextView
变得更加灵活。 Hope this can help someone else too! 希望这也可以帮助其他人!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.