26

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

谢谢阅读。

4

11 回答 11

36

这是我用来检测 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;
}
于 2016-01-20T07:00:45.663 回答
21

如果粘贴板上的句子很长,则检查粘贴板的字符串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
 }
于 2017-08-30T12:08:53.930 回答
16

你的 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

于 2012-09-19T02:30:55.923 回答
6

这是完美的

Xcode 11x 斯威夫特 5x

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

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

于 2018-06-26T12:06:22.580 回答
5

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

public override func paste(_ sender: Any?) 
于 2017-11-10T12:40:20.773 回答
4

它适用于 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
}
于 2019-10-30T08:30:28.903 回答
4

在 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
}
于 2021-02-18T17:36:16.437 回答
3

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;
}
于 2016-04-13T08:21:00.627 回答
0

这是我能够让它工作的唯一方法。我使用了 textField,但同样的概念仍然适用于 textView。

在下面的shouldChangeCharactersIn委托方法中,我将string参数转换为 NSString,然后再转换回 String。然后我将它与粘贴的任何内容进行比较。其他所有内容都在代码上方的注释中。

// 1. class property for anything that was copied and will be pasted
var pasted: String?

// 2. When the user first taps the textfield set the above class property to the copied text (if there is any)
func textFieldDidBeginEditing(_ textField: UITextField) {

    // 3. set it here
    pasted = UIPasteboard.general.string // this is what was copied and will be pasted
}

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    guard let safeText = textField.text else { return true }

    let currentString: NSString = safeText as NSString
    let newString: NSString = currentString.replacingCharacters(in: range, with: string) as NSString

    let str = newString as String

    // 4. compare the above str constant to the pasted variable
    if str == self.pasted {
        print("pasted")

    } else {

        print("typed")
    }

    return true
}

func textFieldDidEndEditing(_ textField: UITextField) {

    // 5. when the user is finished with the textField set the pasted property to nil
    pasted = nil
}
于 2021-12-13T19:39:25.550 回答
-1

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

- (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;
}
于 2019-04-11T12:49:47.203 回答
-2

在 SWIFT 4 中:

func textViewDidChange(_ textView: UITextView) {

    if(textView.text == UIPasteboard.general.string)
    {
        //Text pasted
    }
}
于 2017-12-12T04:52:39.690 回答