ios UITextField

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#import "ViewController.h"

@interface ViewController () <UITextFieldDelegate>
@property (strong) IBOutlet UITextField *f;
@end

@implementation ViewController

-(void)viewDidLoad{
[super viewDidLoad];
[self.f addTarget:self
action:@selector(valueChangeCallback:)
forControlEvents:UIControlEventEditingChanged];
}

//文本变更后触发
-(void) valueChangeCallback:(UITextField *) sender{
NSLog(@"f.text %@",sender.text);
}

-(IBAction) changeByCode:(id)sender{
self.f.text = @"1234";
//不会触发下面如何方法
}

#pragma mark - delegate

//清除按钮的回调
-(BOOL)textFieldShouldClear:(UITextField *)textField{
NSLog(@"textFieldShouldClear");
return YES;
}

//Return 按键的回调 返回yes表示 开发人员自己来处理Return时间
//Return yes/no 跟关键键盘没有直接关系
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
NSLog(@"textFieldShouldReturn");
[textField resignFirstResponder];
return NO;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField{

}

//停止编辑前调用 返回yes继续编辑 返回no取消"停止编辑"
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
return YES;
}


//停止编辑后调用
-(void)textFieldDidEndEditing:(UITextField *)textField{

}

//ios 10的方法 替换上面这个方法
-(void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason{
NSLog(@"textFieldDidEndEditing:reason %zd",reason);

}

//文本变更之前的方法
//range是影响的文本范围
//replacementString是range范围文本的替换内容
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSLog(@"-----------------");
NSLog(@"current [%@]",textField.text);
NSLog(@"range %zd, %zd",range.location, range.length);
NSLog(@"replacementString [%@]",string);
NSLog(@"-----------------");
return YES;
}
#pragma mark end -
@end