iOS 7.1 UITextView 在换行后仍然没有滚动到光标/插入符号 [英] iOS 7.1 UITextView still not scrolling to cursor/caret after new line
问题描述
从 iOS 7 开始,UITextView
不会在用户键入文本时自动滚动到光标.这个问题在 SO 和其他地方都有很好的记录.对我来说,这个问题仍然存在于 iOS 7.1 中.我做错了什么?
Since iOS 7, a UITextView
does not scroll automatically to the cursor as the user types text that flows to a new line. This issue is well documented on SO and elsewhere. For me, the issue is still present in iOS 7.1. What am I doing wrong?
我安装了 Xcode 5.1 并针对 iOS 7.1.我正在使用自动布局.
I installed Xcode 5.1 and targeted iOS 7.1. I'm using Auto Layout.
以下是我如何将文本视图的内容放置在键盘上方:
Here's how I position the text view's content above the keyboard:
- (void)keyboardUp:(NSNotification *)notification
{
NSDictionary *info = [notification userInfo];
CGRect keyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
UIEdgeInsets contentInset = self.textView.contentInset;
contentInset.bottom = keyboardRect.size.height;
self.textView.contentInset = contentInset;
}
我尝试过的:我已经尝试了许多发布到 SO 上的解决方案,因为它与 iOS 7 相关.我尝试过的所有解决方案都没有strong> 似乎很适合显示属性字符串的文本视图.在以下三个步骤中,我概述了关于 SO (https://stackoverflow.com/a/19277383/1239263) 响应用户第一次点击返回键.
What I have tried: I have tried many of the solutions posted to SO on this issue as it pertains to iOS 7. All of the solutions that I have tried do not seem to hold up well for text views displaying an attributed string. In the following three steps, I outline how the most up-voted answer on SO (https://stackoverflow.com/a/19277383/1239263) responds to the user tapping the return key for the first time.
(1.) 文本视图成为viewDidLoad
中的第一响应者.滚动到光标所在文本视图的底部.
(1.) The text view became the first responder in viewDidLoad
. Scroll to the bottom of the text view where the cursor is located.
(2.) 在输入单个字符之前,点击键盘上的返回键.插入符号消失在视线之外.
(2.) Before typing a single character, tap the return key on the keyboard. The caret disappears out of sight.
(3.) 然而,再次点击返回键似乎使情况正常化.(注意:然而,删除后一个新行会使插入符号再次消失).
(3.) Tapping the return key again, however, seems to normalize the situation. (Note: deleting the latter new line, however, makes the caret disappear once again).
推荐答案
改进了 UITextView
后代类的解决方案代码:
Improved solution's code for UITextView
descendant class:
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define is_iOS7 SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")
#define is_iOS8 SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")
@implementation MyTextView {
BOOL settingText;
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextViewDidChangeNotification:) name:UITextViewTextDidChangeNotification object:self];
}
return self;
}
- (void)scrollToCaretInTextView:(UITextView *)textView animated:(BOOL)animated {
CGRect rect = [textView caretRectForPosition:textView.selectedTextRange.end];
rect.size.height += textView.textContainerInset.bottom;
[textView scrollRectToVisible:rect animated:animated];
}
- (void)handleTextViewDidChangeNotification:(NSNotification *)notification {
if (notification.object == self && is_iOS7 && !is_iOS8 && !settingText) {
UITextView *textView = self;
if ([textView.text hasSuffix:@"
"]) {
[CATransaction setCompletionBlock:^{
[self scrollToCaretInTextView:textView animated:NO];
}];
} else {
[self scrollToCaretInTextView:textView animated:NO];
}
}
}
- (void)setText:(NSString *)text {
settingText = YES;
[super setText:text];
settingText = NO;
}
请注意,在蓝牙键盘上按下向下键时它不起作用.
Note it doesn't work when Down key is pressed on Bluetooth keyboard.
这篇关于iOS 7.1 UITextView 在换行后仍然没有滚动到光标/插入符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!