我正在开发 iPhone 中的文本转语音应用程序,
其中有一个接受输入的文本字段,我希望用户从文本字段中选择文本的一部分,我的应用程序会将所选文本转换为语音。
我的问题是如何获取用户从文本字段中选择的文本?
最佳答案
-[UITextField selectedText]
尽管UITextField
没有 selectedText
方法,它符合 UITextInput
protocol .因此,您可以使用 UITextInput
协议(protocol)所需的属性和方法来确定 UITextField *textField
的 selectedText
(或任何符合到 UITextInput
协议(protocol),例如 UITextView
)。
NSString *selectedText = [textField textInRange:textField.selectedTextRange];
NSLog(@"selectedText: %@", selectedText);
另外,您还可以使用 UITextInput
所需的属性和方法来计算 UITextField *textField
的 selectedRange
。
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/