简体   繁体   中英

how to get selected text from uitextfield in iphone?

I am working on Text to speech application in iPhone,

in which have a text field that takes input, i want user to select some portion of text from text field and my application will convert that selected text into speech.

my problem is how would i get the text that user has selected from text field?

-[UITextField selectedText]

Although UITextField doesn't have a selectedText method, it conforms to the UITextInput protocol . So, you can use the required properties & methods of the UITextInput protocol to determine the selectedText of a UITextField *textField (or any object that conforms to the UITextInput protocol, such as a UITextView ).

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

As an aside, you can also use the required properties & methods of the UITextInput to calculate the selectedRange of 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 doesn't declare a textFieldDidChangeSelection: delegate method like -[UITextViewDelegate textViewDidChangeSelection:] , you can still hook into when the selection of a UITextField has changed. To do so, subclass UITextField and use method swizzling to add your own code to the textField.inputDelegate 's native implementation of -[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'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<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);        
}

I did solve my query as follow :

I implement UITextView's delegate and implement 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
}

That's it!

Swift

In Swift, getting the selected text from a UITextField is done like this:

if let textRange = myTextField.selectedTextRange {

    let selectedText = myTextField.textInRange(textRange)

}

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

A similar topic is discussed here: Can I select a specific block of text in a UITextField?

AFAIK there is no event if text is selected. However, you could setup an NSTimer to watch your textfield and check the _selectedRange . If it changes, go fire up your text-to-speech code.

EDIT : I was wrong about the selection. UITextField cannot do what you want to achieve. But if you use UITextView instead, you can implement its UITextViewDelegate and override

- (void)textViewDidChangeSelection:(UITextView *)textView

In there, you can use the selectedRange poperty to get the selection. See this reference for details:

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

UITextField don't have delegate to get the selection range change. We can use KVO to observe selectedTextRange property of UITextfield .

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

Or create UITextField subclass and override setSelectedTextRange method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM