问题描述
我必须在滚动的UITextView中更新少量的文本。我只会在当前光标处插入一个字符,我将在导航栏上按下一个按钮。
I have to update a small amount of text in a scrolling UITextView. I'll only be inserting a character where the cursor currently is, and I'll be doing this on a press of a button on my navigation bar.
我的问题是每当我调用文本视图的setText方法时,它跳转到文本的底部。我试过使用contentOffset和重置selectedRange,但它不工作!这里是我的例子:
My problem is that whenever I call the setText method of the text view, it jumps to the bottom of the text. I've tried using contentOffset and resetting the selectedRange but it doesn't work! Here's my example:
// Remember offset and selection
CGPoint contentOffset = [entryTextView contentOffset];
NSRange selectedRange = [entryTextView selectedRange];
// Update text
entryTextView.text = entryTextView.text;
// Try and reset offset and selection
[entryTextView setContentOffset:contentOffset animated:NO];
[entryTextView setSelectedRange: selectedRange];
有没有办法可以更新文本而没有任何滚动移动...
Is there any way you can update the text without any scroll movement at all... as if they'd just typed something on the keyboard?
非常感谢,
Michael
编辑:
我尝试过使用textViewDidChange:delegate方法,但它仍然没有滚动到原始位置。
I've tried using the textViewDidChange: delegate method but it's still not scrolling up to the original location.
- (void)textViewDidChange:(UITextView *)textView {
if (self.programChanged) {
[textView setSelectedRange:self.selectedRange];
[textView setContentOffset:self.contentOffset animated:NO];
self.programChanged = NO;
}
}
- (void)changeButtonPressed:(id)sender {
// Remember position
self.programChanged = YES;
self.contentOffset = [entryTextView contentOffset];
self.selectedRange = [entryTextView selectedRange];
// Update text
entryTextView.text = entryTextView.text;
}
推荐答案
如果您使用iPhone 3.0稍后,您可以解决此问题:
If you use iPhone 3.0 or later, you can solve this problem:
textView.scrollEnabled = NO;
//You should know where the cursor will be(if you update your text by appending/inserting/deleting you can know the selected range) so keep it in a NSRange variable.
Then update text:
textView.text = yourText;
textView.scrollEnabled = YES;
textView.selectedRange = range;//you keep before
更多跳跃)
回馈
Meir Assayag
RegardsMeir Assayag
这篇关于在编程设置文本时停止UITextView跳转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!