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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12454857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:32:55  来源:igfitidea点击:

how to know when text is pasted into UITextView

xcodeuitextview

提问by Mrwolfy

What event is fired when a block of text is pasted into a UITextView? I need to modify the frame of my textView when the text is pasted in.

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

Thanks for reading.

谢谢阅读。

采纳答案by joseph.hainline

Your UITextView will call its UITextViewDelegate method

你的 UITextView 会调用它的 UITextViewDelegate 方法

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

if a delegate has been set up. This gets called both when a character is typed on the keyboard, and when text is pasted into the text view. The text pasted in is the replacementText argument.

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

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

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

回答by carlos16196

Here is what i use to detect paste events in UITextView:

这是我用来检测 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;
}

回答by Takamitsu Mizutori

Checking the pasteboard's string by if string == UIPasteboard.general.stringtakes a couple of seconds if you have long sentence in the pasteboard. The user sees the keypad is frozen while this check. My solution is to check if the length of new characters is longer than 1. If it is longer than 1, the string is from the pasteboard.

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
 }

回答by GovindhaRaj

This working Perfect Xcode 9.4 Swift 4.1

这个工作完美的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
}

When ever the user try to Pasteinto text field the if condition will execute
This code will stop pasting

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

回答by danielrosero

carlos16196 was a good approach, but I would also tweak it by changing [text isEqualToString:[UIPasteboard generalPasteboard].string]to [text containsString:[UIPasteboard generalPasteboard].string]

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

By doing this, you will detect when user pastes in the textview after other typed text that is not in the UIPasteboard.

通过这样做,您将检测用户何时在 UIPasteboard 中没有的其他键入文本之后粘贴文本视图。

This is the code:

这是代码:

-(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;
}

回答by slboat

try subclasses UITextview,and override this function.

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

public override func paste(_ sender: Any?) 

回答by User18474728

It is for Swift5.1

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

回答by Joseph Razon IL

This is what I use to detect pasted images:

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

- (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;
}

回答by Nupur Sharma

In SWIFT 4:

在 SWIFT 4 中:

func textViewDidChange(_ textView: UITextView) {

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