How to find out when text is inserted into a UITextView

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

Thank you for reading.

+14
source share
8 answers

Your UITextView will call its UITextViewDelegate method

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

if the delegate is configured. This is called both when you hover a character on the keyboard, and when inserting text into a text view. Nested text is the replaceText argument.

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

+13
source

Here is what I use to detect insert events in a 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; } 
+31
source

Checking a cardboard string for if string == UIPasteboard.general.string takes a couple of seconds if you have a long sentence in a file cabinet. The user sees that the keyboard is locked during this check. My solution is to check if the length of the new characters is greater than 1. If the length is greater than 1, the line is output from cardboard.

 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 } 
+14
source

This is 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 a user tries to insert into a text field, the if condition will be met
This code will stop embedding

+5
source

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

This way you will find when the user inserts text into the text box after another typed text that is not in the UIP file.

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; } 
+4
source

try subclassing UITextview and override this function.

 public override func paste(_ sender: Any?) 
+2
source

Here is what I use to detect inserted 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
source

In SWIFT 4:

 func textViewDidChange(_ textView: UITextView) { if(textView.text == UIPasteboard.general.string) { //Text pasted } } 
-3
source

Source: https://habr.com/ru/post/925551/


All Articles