我找到了解决方案。这是一个黑客,但我会尽我所能。
我发现当用户点击 PDF 文本字段时,PDFKit 会隐藏 PDF 文本字段并在同一位置覆盖 UITextView。它使 UITextView 成为第一响应者并调出键盘。该 UITextView 一直存在,直到用户在其他地方点击,当它被删除并替换为包含(现已失效的)UITextView 内容的 PDF 文本字段时。
有问题的 UITextView 深埋在 PDFView 的私有 UIView 子类中。
下面是我正在使用的代码。它从一个视图(PDFView)开始,然后深入寻找它可以找到的任何 UITextView。找到后,它辞去第一响应者的身份,更改参数,并再次成为第一响应者。用户将看到预输入按钮短暂出现然后消失。我还没有找到解决这个问题的方法,因为我们无法访问 UITextView,直到它已经是第一响应者。
此处的代码通过每 0.1 秒执行一次的计时器调用。我确信有更有效的方法可以做到这一点,但这很有效,并且几乎不会在 CPU 仪表上注册。
此代码还设置了 UITextView 的 pasteDelegate,因为在我的情况下,我想覆盖并防止将文本粘贴到 UITextView 中。执行此操作的代码很简单;只是textPasteConfigurationSupporting
回报。[item setNoResult]
与所有类似的 hack 一样,请务必使用您的应用支持的所有 iOS 版本进行测试——包括未来版本。Apple 可以轻松更改其 PDFKit 实现,从而导致其中断或行为不端。或者更好的是,他们可以添加支持的方法来执行此操作。
-(void)lookForTextViewsInView:(UIView *)view
for (UIView *subview in view.subviews) {
if ([subview isKindOfClass:[UITextView class]]) {
UITextView *textView = (UITextView *)subview;
//NSLog(@"Found text field with contents: %@",textView.text);
if (textView.autocapitalizationType == UITextAutocapitalizationTypeNone &&
textView.autocorrectionType == UITextAutocorrectionTypeNo &&
textView.spellCheckingType == UITextSpellCheckingTypeNo &&
textView.pasteDelegate == self) {
//NSLog(@"textView %@ is already adjusted", textView.text);
return;
}
if (textView.isFirstResponder) {
//NSLog(@"Adjusting and resetting first responder of %@",textView.text);
[textView resignFirstResponder];
textView.autocapitalizationType = UITextAutocapitalizationTypeNone;
textView.autocorrectionType = UITextAutocorrectionTypeNo;
textView.spellCheckingType = UITextSpellCheckingTypeNo;
textView.pasteDelegate = self;
[textView becomeFirstResponder];
} else {
//I don't think this ever fires, but here for completion's sake
//NSLog(@"Adjusting without resetting first responder of %@",textView.text);
textView.autocapitalizationType = UITextAutocapitalizationTypeNone;
textView.autocorrectionType = UITextAutocorrectionTypeNo;
textView.spellCheckingType = UITextSpellCheckingTypeNo;
textView.pasteDelegate = self;
}
} else {
//NSLog(@"%@ is not a UITextView", [subview class]);
[self lookForTextViewsInView:subview];
}
}
}