How to get selected text from uitextfield in iphone?

I am working on a text application on the iPhone,

in which there is a text field that accepts input, I want the user to select part of the text from the text field, and my application will convert the selected text to speech.

My problem is how to get the text that the user selected from the text box?

+18
ios iphone textfield
Mar 08 2018-11-11T00:
source share
5 answers

-[UITextField selectedText]

Although UITextField does not have a selectedText method, it conforms to the UITextInput protocol. Thus, you can use the necessary properties and methods of the UITextInput protocol to determine selectedText for UITextField *textField (or any object that conforms to the UITextInput protocol, such as UITextView ).

 NSString *selectedText = [textField textInRange:textField.selectedTextRange]; NSLog(@"selectedText: %@", selectedText); 

As an aside, you can also use the necessary properties and methods of UITextInput to compute selectedRange a UITextField *textField .

 UITextRange *selectedTextRange = textField.selectedTextRange; NSUInteger location = [textField offsetFromPosition:textField.beginningOfDocument toPosition:selectedTextRange.start]; NSUInteger length = [textField offsetFromPosition:selectedTextRange.start toPosition:selectedTextRange.end]; NSRange selectedRange = NSMakeRange(location, length); NSLog(@"selectedRange: %@", NSStringFromRange(selectedRange)); 

-[UITextFieldDelegate textFieldDidChangeSelection:]

Although UITextFieldDelegate does not declare the delegate method textFieldDidChangeSelection: for example -[UITextViewDelegate textViewDidChangeSelection:] , you can still connect when the UITextField selection UITextField changed. To do this, subclass UITextField and use the swizzling method to add your own code to your own implementation of textField.inputDelegate -[UITextInputDelegate selectionDidChange:] .

 // MyTextField.h #import <UIKit/UIKit.h> @interface MyTextField : UITextField @end // MyTextField.m #import <objc/runtime.h> #import "MyTextField.h" UIKIT_STATIC_INLINE void mySelectionDidChange(id self, SEL _cmd, id<UITextInput> textInput); @implementation MyTextField { BOOL swizzled; } #pragma mark - UIResponder // Swizzle here because self.inputDelegate is set after becomeFirstResponder gets called. - (BOOL)becomeFirstResponder { if ([super becomeFirstResponder]) { [self swizzleSelectionDidChange:YES]; return YES; } else { return NO; } } // Unswizzle here because self.inputDelegate may become the inputDelegate for another UITextField. - (BOOL)resignFirstResponder { if ([super resignFirstResponder]) { [self swizzleSelectionDidChange:NO]; return YES; } else { return NO; } } #pragma mark - Swizzle -[UITextInput selectionDidChange:] // Swizzle selectionDidChange: to "do whatever you want" when the text field selection has changed. // Only call this method on the main (UI) thread because it may not be thread safe. - (void)swizzleSelectionDidChange:(BOOL)swizzle { if (swizzle == swizzled || ![self respondsToSelector:@selector(inputDelegate)]) return; // 4.3 Class inputDelegateClass = object_getClass(self.inputDelegate); SEL mySelector = @selector(mySelectionDidChange:); class_addMethod(inputDelegateClass, mySelector, (IMP)mySelectionDidChange, "v@:@"); Method myMethod = class_getInstanceMethod(inputDelegateClass, mySelector); Method uiKitMethod = class_getInstanceMethod(inputDelegateClass, @selector(selectionDidChange:)); method_exchangeImplementations(uiKitMethod, myMethod); swizzled = swizzle; // NSLog(@"swizzled? %i", method_getImplementation(uiKitMethod) == (IMP)venmo_selectionDidChange); } @end UIKIT_STATIC_INLINE void mySelectionDidChange(id self, SEL _cmd, id<UITextInput> textInput) { // Call the native implementation of selectionDidChange:. [self performSelector:@selector(mySelectionDidChange:) withObject:textInput]; // "Do whatever you want" with the selectedText below. NSString *selectedText = [textInput textInRange:textInput.selectedTextRange]; NSLog(@"selectedText: %@", selectedText); } 
+28
Jul 06 2018-12-12T00:
source share

I fulfilled my request as follows:

I implement the UITextView delegate and implement the following method

 - (void)textViewDidChangeSelection:(UITextView *)textView { NSRange r = textView.selectedRange; NSLog(@"Start from : %d",r.location); //starting selection in text selection NSLog(@"To : %d",r.length); // end position in text selection NSLog([tv.text substringWithRange:NSMakeRange(r.location, r.length)]); //tv is my text view } 

What is it!

+6
Mar 08 2018-11-11T00:
source share

Swift

In Swift, getting selected text from a UITextField is done as follows:

 if let textRange = myTextField.selectedTextRange { let selectedText = myTextField.textInRange(textRange) } 

where textRange is a UITextRange that is used to get the actual selected text.

+2
Jun 08 '16 at 2:46 on
source share

A similar topic is discussed here: Is it possible to select a specific block of text in a UITextField?

AFAIK no event if text is selected. However, you can configure NSTimer to view the text field and check _selectedRange . If it changes, run the text-to-speech code.

EDIT : I was mistaken in the choice. UITextField cannot do what you want to achieve. But if you use a UITextView instead, you can implement its UITextViewDelegate and override

 - (void)textViewDidChangeSelection:(UITextView *)textView 

In it, you can use selectedRange poperty to get the selection. See this link for more details:

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextView_Class/Reference/UITextView.html#//apple_ref/doc/uid/TP40006898-CH3-SW13

+1
Mar 08 2018-11-11T00:
source share

UITextField does not have a delegate to change the selection range. We can use KVO to observe the selectedTextRange property of UITextfield .

  [textField addObserver:self forKeyPath:@"selectedTextRange" options:NSKeyValueObservingOptionNew context:NULL]; 

Or, subclass UITextField and override the setSelectedTextRange method.

0
May 6 '19 at 6:51
source share



All Articles