美文网首页
iOS16适配:UIFindInteraction的使用

iOS16适配:UIFindInteraction的使用

作者: 喔牛慢慢爬 | 来源:发表于2022-10-19 14:26 被阅读0次

    UIFindInteraction是iOS16中新增的文本查找交互功能,用于文本内容的查找与替换,它会弹出一个查找面板(键盘上方)输入需要搜索的关键字即可进行查找与替换操作。现有UITextView,WKWebView,PDFView已经默认支持了,使用时需要将isFindInteractionEnabled属性设置为true

    Simulator Screen Shot - iPhone 14 Pro - 2022-10-19 at 14.29.25.png
    import UIKit
    
    class ViewController: UIViewController {
    
        lazy var textView: UITextView = {
            let _textView = UITextView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 600.0))
            _textView.text = "为了给您提供更好的内容和产品体验,我们诚邀您参与用户调研!希望您能抽出几分钟时间,将您的感受和建议告诉我们,我们非常重视用户的宝贵意见,期待您的参与!"
            _textView.center = self.view.center
            //打开UIFindInteraction
            if #available(iOS 16.0, *) {
                _textView.isFindInteractionEnabled = true
                //预填充查找字段
                _textView.findInteraction?.searchText = "哈哈哈"
                //预填充系统查找面板的替换文本字段
                _textView.findInteraction?.replacementText = "你好,你好"
            }
            let _longPress = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress))
            _textView.addGestureRecognizer(_longPress)
            return _textView
        }()
        
        
        override func viewDidLoad() {
            super.viewDidLoad()
            self.view.addSubview(self.textView)
        }
        
        @objc func handleLongPress(_ recognizer: UIGestureRecognizer) {
            if #available(iOS 16.0, *) {
                self.textView.findInteraction?.presentFindNavigator(showingReplace: true)
            } else {
                // Fallback on earlier versions
            }
        }
    
        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            if #available(iOS 16.0, *) {
                self.textView.findInteraction?.dismissFindNavigator()
            } else {
                // Fallback on earlier versions
            }
        }
    
    }
    

    相关文章

      网友评论

          本文标题:iOS16适配:UIFindInteraction的使用

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