【问题标题】:UITextView paste method overrideUITextView粘贴方法覆盖
【发布时间】:2011-01-19 14:53:35
【问题描述】:

我真的希望能够检测到 UITextView 中的粘贴事件,但是这似乎无法做到。

我最初尝试继承 UITextView 并覆盖 paste: 方法,但它从未在粘贴事件上被调用。

有人能做到吗?之前关于同一个问题的问题在 8 月没有答案......

【问题讨论】:

    标签: iphone uitextview paste


    【解决方案1】:

    文本视图没有捕捉到paste: 事件,因为它不是真正的响应者不是文本视图,而是为文本视图提供动力的私有 Web 视图 (UIWebDocumentView)。

    但是,在粘贴时,Web 视图将调用文本视图的(私有)-[UITextView keyboardInput:shouldInsertText:isMarkedText:],进而调用文本视图的委托-textView:shouldChangeTextInRange:replacementText:

    因此,您只需在文本视图的委托中实现-textView:shouldChangeTextInRange:replacementText:

    (当然,正常的键盘输入也会触发这个方法,没有完美的区分方式。)

    【讨论】:

      【解决方案2】:

      @KennyTM 我为我的一个应用程序所做的是跟上当前文本长度和以前的文本长度。如果 (currentTextLength - previousTextLength) 大于 1,那么用户一定粘贴了一些东西

      【讨论】:

        【解决方案3】:

        对于 iOS 14,您必须分两部分执行此操作,以避免向用户显示您正在检查 UIPasteboard 的通知。就我而言,我不想对用户数据做任何坏事,但我确实想在用户粘贴到 UITextView 时进行一些特殊的格式化。

        第 1 步:创建自定义 UITextView 并覆盖 paste()

        import UIKit
        
        protocol TouchableTextViewDelegate : class{
            func touchesDidBegin()
            func pasting()
        }
        
        class TouchableTextView: UITextView {
            weak var touchableDelegate : TouchableTextViewDelegate?
        
            
            override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
                if self.isFirstResponder{
                    return true
                }
                touchableDelegate?.touchesDidBegin()
                return false
            }
            
            override func paste(_ sender: Any?) {
                touchableDelegate?.pasting()
                super.paste(sender)
            }
            
        }
        

        第 2 步: 在您处理 shouldChangeTextIn 的文件位置创建一个变量,并确保为 TouchableTextView 设置委托。就我而言

        //top of the view
        var isPasting : Bool = false
        
        //also when creating UITextView use both delegates
        textView.touchableDelegate = self
        //add the normal delegate
        textView.delegate = self
        
        extension SliderTextView : TouchableTextViewDelegate{
            func pasting() {
                self.isPaste = true
            }
            
            func touchesDidBegin() {
                sliderEditingDelegate?.touchesDidBegin(sliderTextView: self)
            }
        }
        

        第 3 步:在 shouldChangeTextIn 内部我处理这样的动作

        func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
             let isPaste = self.isPaste
             //be sure to set this to false
             self.isPaste = false
             if isPaste,
                let pt = UIPasteboard.general.string,
                text.contains(pt){ 
                //you will see the paste notification and that is good for the user
                // but only when the user pastes
                // do whatever special thing or formatting you want to do
        
              }   
              return true
        }
        

        好处是除非用户在 UITextView 中粘贴,否则您不会触发通知。

        【讨论】:

          【解决方案4】:

          要检测用户是否正在解析 textView 中的文本,请将 shouldChangeTextInRange 委托中的 replacementText 与用户当前在 UIPasteboard 中持有的文本进行比较。然后根据需求采取行动。

          有关代码,请参阅我在以下问题中的回答:

          how to know when text is pasted into UITextView

          【讨论】:

            猜你喜欢
            • 2011-12-12
            • 1970-01-01
            • 1970-01-01
            • 2019-09-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-04-13
            • 2010-09-22
            相关资源
            最近更新 更多