UITextField 继承于 UIControl .
特殊的技能是可以输入.
// 输入框
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 50, 200, 40)];
textField.backgroundColor = [UIColor whiteColor];
[self.window addSubview:textField];
[textField release];
textField.layer.borderWidth = 1;
textField.layer.cornerRadius = 10;
// 初始文本
//textField.text = @"贱男";
textField.textColor = [UIColor purpleColor];
// 占位文本
textField.placeholder = @"请输入姓名";
textField.textAlignment = NSTextAlignmentCenter;
textField.keyboardType = UIKeyboardTypeDefault;
// return 按钮切换样式
textField.returnKeyType = UIReturnKeyDone;
textField.clearsOnBeginEditing = YES;
//textField.secureTextEntry = YES;
// 清除按钮的样式
textField.clearButtonMode = UITextFieldViewModeAlways;
// 创建一个view
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 20, 20)];
view.backgroundColor = [UIColor orangeColor];
// 弹出一个自定义的视图,默认是键盘
textField.inputView = view;
// 给键盘添加一个辅助视图.
textField.inputAccessoryView = view;
// 给textfield设置代理人.
textField.delegate = self;
// 点击Return 按钮,回收键盘.(协议)
下面的方法是:
// 实现协议方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
NSLog(@"测试return按钮");
NSLog(@"%@",textField.text);
// 这句话是回收return键盘
[textField resignFirstResponder];
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
NSLog(@"测试清除按钮");
return YES;
}
// UITextField 继承于UIControl 所以也有这个点击方法.
- (void)click:(UITextField *)textField{
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-12 15:04:38