UITextField輸入漢字時(shí)赛蔫,在輸入框會(huì)變成拼音的英文字母裤唠。比如輸入chang(長(zhǎng))之后挤牛,輸入qi(期),此時(shí)輸入框會(huì)顯示“長(zhǎng)prg期”种蘸。通過(guò)增加輸入框的監(jiān)聽(tīng)方法墓赴,對(duì)輸入內(nèi)容進(jìn)行控制竞膳。
#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)//點(diǎn)擊完選中的字之后
{
NSLog(@"text:%@", textField.text);
}
else//沒(méi)有點(diǎn)擊出現(xiàn)的漢字,一直在點(diǎn)擊鍵盤
{
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)//點(diǎn)擊完選中的字之后
{
NSLog(@"text:%@", textField.text);
self.input.text = textField.text;
}
else//沒(méi)有點(diǎn)擊出現(xiàn)的漢字,一直在點(diǎn)擊鍵盤
{
NSLog(@"markedTextRange:%@",textField.text);
}
}
@end
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者