how to know when text is pasted into UITextView

后端 未结 9 469
暗喜
暗喜 2020-11-30 10:15

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.

Thanks for reading.

相关标签:
9条回答
  • 2020-11-30 10:30

    In SWIFT 4:

    func textViewDidChange(_ textView: UITextView) {
    
        if(textView.text == UIPasteboard.general.string)
        {
            //Text pasted
        }
    }
    
    0 讨论(0)
  • 2020-11-30 10:31

    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;
    }
    
    0 讨论(0)
  • 2020-11-30 10:41

    This working Perfect 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 Paste into text field the if condition will execute
    This code will stop pasting

    0 讨论(0)
提交回复
热议问题