美文网首页Swift 5.x 学习之旅iOS开发总结
iOS 15 UITextField长按菜单支持live tex

iOS 15 UITextField长按菜单支持live tex

作者: Auditore | 来源:发表于2022-10-14 17:11 被阅读0次

    1.需求背景

    iOS15上有一个非常好用的scan text (也可以叫live text)功能,如果能加上,用于扫描文本OCR(光学字符识别Optical Character Recognition),直接插入到textField里还是很方便的。

    ios15-iphone12-pro-photos-copy-text.png

    2.为什么目前不支持?

    因为我用的自定义textField,没有针对scan text进行适配。我查阅了很多关于iOS15 OCR的资料。但是给textField长按菜单,加上scan text的内容没有找到,大体都说自定义长按菜单需要在

    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool

    这个函数里去添加对应的action,然后

    override func target(forAction action: Selector, withSender sender: Any?) -> Any?

    这个函数里也要去做对应方法的实现,才能有对应的功能。

    那么scan text对应的是哪个selector方法呢?

    UIResponderStandardEditActions这个类里,并没有看到和OCR或者scan text相关的方法。然后我翻阅官方文档,找到了个很类似OCR的API

    extension UIResponder {
        @available(iOS 15.0, *)
        open func captureTextFromCamera(_ sender: Any?)
    }
    

    试了下,果然就是这个玩意儿

    所以如果你要你的自定义textField或者textview拥有scan text OCR的能力,那么应该如下实现。

    3.实现

    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if #available(iOS 15.0, *) {
            if action == #selector(UIResponderStandardEditActions.paste(_:)) ||
                action == #selector(UIResponder.captureTextFromCamera(_:)){
                return true
            }
        } else {
            return super.canPerformAction(action, withSender: sender)
        }
        return super.canPerformAction(action, withSender: sender)
    }
    
    override func target(forAction action: Selector, withSender sender: Any?) -> Any? {
        if #available(iOS 15.0, *) {
            if action == #selector(UIResponderStandardEditActions.paste(_:)) ||
                action == #selector(UIResponderStandardEditActions.select(_:)) ||
                action == #selector(UIResponderStandardEditActions.selectAll(_:)) ||
                action == #selector(UIResponder.captureTextFromCamera(_:)) {
                return super.target(forAction: action, withSender: sender)
            }
        } else {
            // Fallback on earlier versions
            if action == #selector(UIResponderStandardEditActions.paste(_:)) ||
                action == #selector(UIResponderStandardEditActions.select(_:)) ||
                action == #selector(UIResponderStandardEditActions.selectAll(_:))
                {
                return super.target(forAction: action, withSender: sender)
            }
        }
        return nil
    }
    

    4.限制

    4.1 版本和设备限制

    只支持iOS15+,且iPhone Xs及以后的设备。如下是官方说明

    For this property to be true, the device must have the A12 Bionic chip or later.

    所以我们需要判断iOS 系统版本是iOS15,且支持OCR。

    4.2 如何判断是否支持live text

    一种是ImageAnalyzer.isSupported,如果是iOS 16就用这个判断,需要引入import VisionKit 这个visionKit。

    另一种是利用实例化方法, canPerformAction(_:withSender:) ,官方的说法如下:

    To determine whether the data scanner runs on the user’s device, pass captureTextFromCamera(_:) to the canPerformAction(_:withSender:) method.

    例如

    if UITextField().canPerformAction(#selector(UIResponder.captureTextFromCamera(_:)), withSender: self) { ... }

    我之前以为直接用button的实例,来判断就可以了,居然不生效。一直返回false,我还不清楚说明原因。但是用上面这种,直接声明一个UITextField的实例来判断,就正常了,真的神奇。如果是在UIController里面,用下面的代码也可以:

    if #available(iOS 15.0, *), self.canPerformAction(#selector(UIResponder.captureTextFromCamera(_:)), withSender: self) {
        print("support live text")
        button.addTarget(self, action: #selector(UIResponder.captureTextFromCamera(_:)), for: .touchUpInside)
    } else {
        print("this device is not support live text")
    }
    

    所以这个条件,也是有点迷。

    5.扩展运用 -- 如何自定义一个按钮,调用scan text识别文字功能?

    自定义的按钮,和textField最大的不同,就是textField默认遵循了UIKeyInput or UITextInput 这两个中的某个协议,所以我们自定定义的按钮,那么我们要自己去遵循至少,这两个之一的协议,然后实现对应的代理方法,否则会崩溃。官方说明如下:

    To receive callbacks from the data scanner, the responder should conform to either the UIKeyInput or UITextInput protocol. If it conforms to UIKeyInput, the scanner calls the insertText: protocol method. If it conforms to UITextInput, the scanner calls the setMarkedText(_:selectedRange:) and unmarkText() protocol methods.

    class ViewController: UIViewController, UIKeyInput {
            var hasText: Bool = false
            
        override func viewDidLoad() {
            super.viewDidLoad()
            let button = UIButton.init(frame: CGRect(x: 100, y: 200, width: 100, height: 100))
                    if #available(iOS 15.0, *), self.canPerformAction(#selector(UIResponder.captureTextFromCamera(_:)), withSender: self) {
                print("support live text")
                button.addTarget(self, action: #selector(UIResponder.captureTextFromCamera(_:)), for: .touchUpInside)
            } else {
                print("this device is not support live text")
            }
            button.backgroundColor = .red
            view.addSubview(button)
        }
        
        func deleteBackward() {
            
        }
        
        func insertText(_ text: String) {
            //ORC scan text的 insert Text 按钮回调
            print("OCR 扫描到的文字是: \(text)")
            labelTest.text = text
        }
    }
    

    参考文献:

    https://developer.apple.com/documentation/uikit/uiresponder/3778577-capturetextfromcamera?changes=_5

    https://developer.apple.com/documentation/visionkit/imageanalyzer/3974561-issupported

    https://stackoverflow.com/questions/69702884/how-to-know-a-device-is-available-to-use-capturetextfromcamera-api/73995804#73995804

    相关文章

      网友评论

        本文标题:iOS 15 UITextField长按菜单支持live tex

        本文链接:https://www.haomeiwen.com/subject/xdjczrtx.html