UITextField输入汉字时,在输入框会变成拼音的英文字母。比如输入chang(长)之后,输入qi(期),此时输入框会显示“长prg期”。通过增加输入框的监听方法,对输入内容进行控制。
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic,strong)UITextField *input;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.input = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, self.view.frame.size.width - 200, 30)];
_input.backgroundColor = [UIColor yellowColor];
[self.view addSubview:self.input];
[self.input addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}
- (void)textFieldDidChange:(UITextField *)textField{
if (textField.markedTextRange == nil)//点击完选中的字之后
{
NSLog(@"text:%@", textField.text);
}
else//没有点击出现的汉字,一直在点击键盘
{
NSLog(@"markedTextRange:%@",textField.text);
self.input.text = textField.text;
}
}
@end
这样改之后输入正常。
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic,strong)UITextField *input;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.input = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, self.view.frame.size.width - 200, 30)];
_input.backgroundColor = [UIColor yellowColor];
[self.view addSubview:self.input];
[self.input addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}
- (void)textFieldDidChange:(UITextField *)textField{
if (textField.markedTextRange == nil)//点击完选中的字之后
{
NSLog(@"text:%@", textField.text);
self.input.text = textField.text;
}
else//没有点击出现的汉字,一直在点击键盘
{
NSLog(@"markedTextRange:%@",textField.text);
}
}
@end