Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h3><code>-[UITextField selectedText]</code></h3> <p>Although <a href="http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITextField_Class/Reference/UITextField.html"><code>UITextField</code></a> doesn't have a <code>selectedText</code> method, it conforms to the <a href="http://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UITextInput_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UITextInput"><code>UITextInput</code> protocol</a>. So, you can use the required properties &amp; methods of the <code>UITextInput</code> protocol to determine the <code>selectedText</code> of a <code>UITextField *textField</code> (or any object that conforms to the <code>UITextInput</code> protocol, such as a <a href="http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITextView_Class/Reference/UITextView.html"><code>UITextView</code></a>).</p> <pre><code>NSString *selectedText = [textField textInRange:textField.selectedTextRange]; NSLog(@"selectedText: %@", selectedText); </code></pre> <p>As an aside, you can also use the required properties &amp; methods of the <code>UITextInput</code> to calculate the <code>selectedRange</code> of a <code>UITextField *textField</code>.</p> <pre><code>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)); </code></pre> <h3><code>-[UITextFieldDelegate textFieldDidChangeSelection:]</code></h3> <p>Although, <code>UITextFieldDelegate</code> doesn't declare a <code>textFieldDidChangeSelection:</code> delegate method like <a href="http://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UITextViewDelegate_Protocol/Reference/UITextViewDelegate.html#//apple_ref/occ/intfm/UITextViewDelegate/textViewDidChangeSelection%3a"><code>-[UITextViewDelegate textViewDidChangeSelection:]</code></a>, you can still hook into when the selection of a <code>UITextField</code> has changed. To do so, subclass <code>UITextField</code> and use method swizzling to add your own code to the <code>textField.inputDelegate</code>'s native implementation of <a href="http://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UITextInputDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITextInputDelegate/selectionDidChange%3a"><code>-[UITextInputDelegate selectionDidChange:]</code></a>.</p> <pre><code>// MyTextField.h #import &lt;UIKit/UIKit.h&gt; @interface MyTextField : UITextField @end // MyTextField.m #import &lt;objc/runtime.h&gt; #import "MyTextField.h" UIKIT_STATIC_INLINE void mySelectionDidChange(id self, SEL _cmd, id&lt;UITextInput&gt; 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's 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&lt;UITextInput&gt; 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); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload