如何知道何时将文本粘贴到UITextView中

Mrw*_*lfy 16 xcode uitextview

将一块文本粘贴到UITextView时会触发什么事件?我需要在粘贴文本时修改textView的框架.

谢谢阅读.

car*_*196 31

这是我用来检测UITextView中的粘贴事件:

 // Set this class to be the delegate of the UITextView. Now when a user will paste a text in that textview, this delegate will be called.
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

    // Here we check if the replacement text is equal to the string we are currently holding in the paste board
    if ([text isEqualToString:[UIPasteboard generalPasteboard].string]) {

        // code to execute in case user is using paste

    } else {

        // code to execute other wise
    }

    return YES;
}
Run Code Online (Sandbox Code Playgroud)

  • 您可能不想执行这种技巧 - iOS 14 将显示一条通知,指示应用程序访问了粘贴板,并且通过这种方法,每次文本视图更改都会发生这种情况。 (12认同)

jos*_*ine 14

您的UITextView将调用其UITextViewDelegate方法

 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
Run Code Online (Sandbox Code Playgroud)

如果已设置代表.当在键盘上键入字符时以及将文本粘贴到文本视图中时,都会调用此方法.粘贴的文本是replacementText参数.

请参阅http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextViewDelegate_Protocol/Reference/UITextViewDelegate.html#//apple_ref/occ/intf/UITextViewDelegate


Tak*_*ori 14

if string == UIPasteboard.general.string如果在粘贴板中有长句,则检查粘贴板的字符串需要几秒钟.用户看到键盘在此检查时被冻结.我的解决方案是检查新字符的长度是否大于1.如果长度大于1,则字符串来自粘贴板.

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if string.characters.count > 1{
            //User did copy & paste

        }else{
            //User did input by keypad
        }            
         return true
 }
Run Code Online (Sandbox Code Playgroud)

  • 如果用户使用预测文本键入单词,则字符数也将大于 1。 (5认同)
  • 部分工作,但是当用户尝试粘贴单个字符时,它将失败 (3认同)
  • 从技术上讲,这无法检测到用户是在粘贴板上粘贴一个字符(为什么这样做?)还是键入一个字符,但是从所有意图和目的来看,它都能完成任务。这就是我要使用的。 (2认同)

Gov*_*Raj 6

正常工作的完美Xcode 9.4 Swift 4.1

 func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    if text.contains(UIPasteboard.general.string ?? "") {
        return false
    }
    return true
}
Run Code Online (Sandbox Code Playgroud)

每当用户尝试粘贴到文本字段时,if条件都会执行。
此代码将停止粘贴

  • 仅供参考:以这种方式调用“UIPasteboard.general.string”会导致每次用户键入时出现“[CurrentApp] Pasted from [IncomingApp]”提示。 (4认同)
  • 这在 iOS 15.0 上不再可靠地工作,因为“从相机扫描文本”将文本粘贴到文本字段上。 (2认同)

Use*_*728 6

适用于 Swift5.1

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    if let paste = UIPasteboard.general.string, text == paste {
       print("paste")
    } else {
       print("normal typing")
    }
    return true
}
Run Code Online (Sandbox Code Playgroud)


Ale*_*kov 6

在 iOS 14 中,每次应用程序从中获取值时都会触发通知,UIPasteboard.general.string
因此检测用户是否粘贴某些内容的正确方法是覆盖paste(_)函数:

var isPastingContent = false

open override func paste(_ sender: Any?) {
    isPastingContent = true
    super.paste(sender)
}

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    if isPastingContent {
         // do something
    }
    isPastingContent = false
}
Run Code Online (Sandbox Code Playgroud)


slb*_*oat 5

尝试子类 UITextview,并覆盖此功能。

public override func paste(_ sender: Any?) 
Run Code Online (Sandbox Code Playgroud)

  • 别忘了给super打电话。 (2认同)