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

时间:2012-09-17 07:31:36

标签: xcode uitextview

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

感谢阅读。

10 个答案:

答案 0 :(得分:28)

以下是我在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;
}

答案 1 :(得分:13)

如果您在粘贴板中有长句,则按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
 }

答案 2 :(得分:12)

您的UITextView将调用其UITextViewDelegate方法

 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

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

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

答案 3 :(得分:5)

可以正常运行的 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
}

只要用户尝试粘贴到文本字段,if条件就会执行
此代码将停止粘贴

答案 4 :(得分:4)

carlos16196是一个很好的方法,但我也会通过将[text isEqualToString:[UIPasteboard generalPasteboard].string]更改为[text containsString:[UIPasteboard generalPasteboard].string]

进行调整

通过执行此操作,您将检测用户何时在文本视图中粘贴其他不在UIPasteboard中的类型文本。

这是代码:

-(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 containsString:[UIPasteboard generalPasteboard].string]) {

    // code to execute in case user is using paste

} else {

    // code to execute other wise
}

return YES;
}

答案 5 :(得分:2)

尝试子类UITextview,并覆盖此函数。

public override func paste(_ sender: Any?) 

答案 6 :(得分:2)

在 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
}

答案 7 :(得分:0)

这就是我用来检测粘贴图像的方法:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if (UIPasteboard.generalPasteboard.image &&
        [UIPasteboard.generalPasteboard.string.lowercaseString isEqualToString:text.lowercaseString]) {

       //Pasted image

        return NO;
    }

    return YES;
}

答案 8 :(得分:0)

用于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
}

答案 9 :(得分:-3)

在SWIFT 4中:

func textViewDidChange(_ textView: UITextView) {

    if(textView.text == UIPasteboard.general.string)
    {
        //Text pasted
    }
}