2016-03-24 ios UITextField 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475#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 Récent Core Graphics Ancien UITextfield placeholder color