UITextField的相关操作:
1 UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(20, 60, 280, 70)]; 2 //文本输入框,一般用作用户输入账号,密码,个人信息等 3 4 tf.placeholder = @"请输入帐号"; 5 //提示语 6 7 tf.borderStyle = UITextBorderStyleLine; 8 //边框风格 9 10 tf.backgroundColor = [UIColor redColor]; 11 12 tf.background = [UIImage imageNamed:@"aaa"]; 13 //设置背景图片,如果边框风格是圆角,那么背景图失效,否则边框风格失效 14 15 tf.font = [UIFont systemFontOfSize:40]; 16 17 tf.adjustsFontSizeToFitWidth = YES; 18 //当文本的宽度超过tf的宽度时,文字会自动变小(系统默认是有下限的) 19 20 tf.minimumFontSize = 30.0f; 21 //文字变小的下限 22 23 tf.textAlignment = NSTextAlignmentLeft; 24 //左右对齐方式,(和label的设置方式一样) 25 26 tf.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 27 //垂直对齐方式,(btn对齐方式的设置也是用这种方式) 28 29 //tf的左右对齐和垂直对齐在工作中一般都不设置,直接使用默认的 30 31 32 tf.keyboardType = UIKeyboardTypeEmailAddress; 33 //键盘类型 34 35 tf.returnKeyType = UIReturnKeyNext; 36 //return键的类型,(这个属性只是设置外观,和功能毫无关系) 37 38 // tf.secureTextEntry = YES; 39 //安全输入模式(暗文输入) 40 41 tf.tag = 1; 42 43 // tf.clearsOnBeginEditing = YES; 44 //开始编辑时清空tf的文字 45 46 tf.clearButtonMode = UITextFieldViewModeAlways; 47 //设置清空按钮出现的时机 48 49 UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)]; 50 v.backgroundColor = [UIColor blueColor]; 51 tf.leftView = v; 52 [v release]; 53 tf.leftViewMode = UITextFieldViewModeAlways; 54 //tf左边的视图和出现的时机(比如说用户的头像或者一把锁) 55 56 57 UIView *av = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; 58 av.backgroundColor = [UIColor magentaColor]; 59 // tf.inputView = av;//代替键盘(工作中一般不用) 60 61 tf.inputAccessoryView = av; 62 //键盘的附属view(出现在键盘上面)
1 //self.view被点击时触发 2 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 3 { 4 UITextField *tf = (UITextField *)[self.view viewWithTag:1]; 5 6 [tf resignFirstResponder]; 7 //收起键盘 8 }