ios - 如何从 iphone 的 uitextfield 中获取选定的文本?

标签 ios iphone textfield

我正在开发 iPhone 中的文本转语音应用程序,

其中有一个接受输入的文本字段,我希望用户从文本字段中选择文本的一部分,我的应用程序会将所选文本转换为语音。

我的问题是如何获取用户从文本字段中选择的文本?

最佳答案

-[UITextField selectedText]

尽管UITextField没有 selectedText 方法,它符合 UITextInput protocol .因此,您可以使用 UITextInput 协议(protocol)所需的属性和方法来确定 UITextField *textFieldselectedText(或任何符合到 UITextInput 协议(protocol),例如 UITextView )。

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

另外,您还可以使用 UITextInput 所需的属性和方法来计算 UITextField *textFieldselectedRange

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:]

不过,UITextFieldDelegate 没有像 -[UITextViewDelegate textViewDidChangeSelection:] 那样声明 textFieldDidChangeSelection: 委托(delegate)方法,当 UITextField 的选择发生变化时,您仍然可以 Hook 。为此,子类化 UITextField 并使用方法调配将您自己的代码添加到 textField.inputDelegate-[UITextInputDelegate selectionDidChange:] 的 native 实现中.

// 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);        
}

关于ios - 如何从 iphone 的 uitextfield 中获取选定的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5230297/

相关文章:

iphone - iOS 通话中状态栏覆盖顶部导航栏

iphone - 核心音频,Goertzel 算法不起作用

iphone - 单击按钮时 iOS 应用程序崩溃 iPhone

ios - watch 操作系统 2.0 测试版 : access heart beat rate

ios - OTA (Over the air) IOS 7.1 更改 http-https 后仍然无法连接服务器?

iphone - 如果我已经发布的 v1 没有版本化的核心数据模型,我可以使用 "Automatic Lightweight Migration"吗?

swift - 选择文本字段时如何使键盘不出现?

java - 文本框无法正常工作

jquery - 使用 jQuery 更改文本字段焦点顺序

android - OneSignal - 基于标签发送通知