在PDFView表单字段中配置键盘设置 [英] Configuring keyboard settings within PDFView form fields

查看:51
本文介绍了在PDFView表单字段中配置键盘设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,该应用程序从服务器加载PDF文件并在PDFView中显示这些PDF文件.这些文件包含用户要在其中键入的表单字段.很好PDF文件将用于教育环境,其中拼写应该 not 不被自动更正,并且预想性文本应该 不可用.

I'm working on an app which loads PDF files from a server and displays those PDF files within a PDFView. The files contain form fields in which the user is to type. That's works fine. The PDF files are to be used in an education setting, where the spelling should not be autocorrected and predictive text should not be available.

我还没有找到一种方法来禁用PDFView中的UITextField中的 autocorrectionType = false .

I have not found the means to disable autocorrect in a PDFView, along the lines of autocorrectionType = false in a UITextField.

我知道用户可以在设备设置中手动禁用自动更正和联想文字.在这种情况下,这不是可行的选择(可能会引起用户的困惑,并且无法进行验证).我可以确定是否可以在整个应用范围内禁用自动更正.

I'm aware the user can manually disable autocorrection and predictive text in device settings. That's not a viable option in this case (likely user confusion and no means to verify). I'm ok if there's a way to disable autocorrect app-wide.

我们正在内部创建PDF文件,因此在生成文件时可以做些什么是可以的.Adobe Acrobat是表单字段上的拼写检查"选项,但该设置至少在PDFView中无效.

We're creating the PDF files in-house, so we're ok if there's something we can do while generating the files. Adobe Acrobat is a "check spelling" option on form fields, but the setting has no effect, at least within PDFView.

谢谢.

推荐答案

我找到了解决方案.这是骇客,但我会尽我所能.

I found a solution. It's a hack, but I'll take what I can get.

我发现,当用户点击PDF文本字段时,PDFKit会隐藏PDF文本字段并在同一位置覆盖UITextView.它使UITextView成为第一个响应者,并调出键盘.该UITextView一直保留到用户点击其他位置为止,然后将其删除并替换为包含(现在已失效的)UITextView内容的PDF文本字段.

I found that when a user taps a PDF text field, PDFKit hides the PDF text field and overlays a UITextView at the same location. It makes that UITextView the first responder and brings up the keyboard. That UITextView remains until the user taps elsewhere, when it is removed and replaced with a PDF text field containing the contents of the (now dead) UITextView.

有问题的UITextView埋在PDFView的深处,在私有UIView子类中.

The UITextView in question is buried deep inside PDFView, within private UIView subclasses.

下面是我正在使用的代码.它从一个视图(PDFView)开始,然后进行深层查找以查找它可以找到的任何UITextView.找到后,它将辞职为第一响应者,更改参数,然后再次成为第一响应者.用户将看到预先输入按钮短暂出现然后消失.我还没有找到解决这个问题的方法,因为直到UITextView成为第一个响应者,我们才可以访问它.

Below is the code I'm using. It starts with a view (the PDFView) and deep-dives looking for any UITextView it can find. When found, it resigns as first responder, changes parameters, and becomes the first responder again. The user will see the typeahead buttons appear briefly then disappear. I haven't found a way around this, as we don't gain access to the UITextView until it is already the first responder.

此处的代码是通过每0.1秒执行一次的计时器调用的.我敢肯定有更有效的方法可以做到这一点,但这确实可行,并且几乎没有在CPU仪表上注册.

The code here is called via a timer executing every 0.1 seconds. I'm sure there are more efficient ways to do this but this works, and barely registers on the CPU meter.

此代码还设置了UITextView的pasteDelegate,因为在我的情况下,我想覆盖并防止将文本粘贴到UITextView中.这样做的代码很简单;在 textPasteConfigurationSupporting 中,只需返回 [item setNoResult] .

This code also sets the pasteDelegate of the UITextView because in my case I want to override and prevent pasting of text into the UITextView. The code to do that is simple; in textPasteConfigurationSupporting just return [item setNoResult].

与所有此类黑客一样,请确保对您的应用支持的所有iOS版本(包括将来的版本)进行测试.Apple可以轻松更改其PDFKit实现,从而导致其中断或行为异常.或者更好的是,他们可以添加受支持的方法来做到这一点.

As with all hacks like this, be sure to test with all versions of iOS your app supports - including future versions. Apple could easily change their PDFKit implementation causing this to break or misbehave. Or better, they could add a supported means to do this.

-(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];
    }
  }
}

这篇关于在PDFView表单字段中配置键盘设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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