UITextFiled常见基础(持续更新)

在登录界面中,UITextFiled左边添加图片

//设置左边添加的图片
UIImageView  *userImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_user"]];
userImage.frame = CGRectMake(0, 0, CGRectGetHeight(self.userName.frame), CGRectGetHeight(self.userName.frame);
userImage.contentMode = UIViewContentViewModeCenter;
//图片添加到self.userName的左边
self.userName.leftView = userImage;
self.userName.leftViewMode = UITextFiledViewModeAlways;

动态监听UITextFiled的text变化

  • 当动态监听UITextFiled的text变化时,可以使用本地通知事件或add Target来监听
  1. 通过本地通知
// 开始编辑
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(beginEditing) name:UITextFieldTextDidBeginEditingNotification object:self];
// 编辑中
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(editing) name:UITextFieldTextDidEndEditingNotification object:self];
// 结束编辑
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endEditing) name:UITextFieldTextDidChangeNotification object:self];
  1. 通过addTarget:因UITextFiled继承UIControl,可以通过addTarget来监听文本框的变化
// 开始编辑
[self.textFiled addTarget:self action:@selector(beginEditing) forControlEvents:UIControlEventEditingDidBegin];
// 编辑中
    [self.textFiled addTarget:self action:@selector(editing) forControlEvents:UIControlEventEditingChanged];
// 结束编辑
 [self.textFiled addTarget:self action:@selector(endEditing) forControlEvents:UIControlEventEditingDidEnd];
// 'return key' ending editing 
[self.textFiled addTarget:self action:@selector(endOnExit) forControlEvents:UIControlEventEditingDidEndOnExit];

友情资料:iOS_成才录

推荐阅读更多精彩内容