示例代码
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITextFieldDelegate>
{
UITextField* _textField;
}
@property (retain, nonatomic) UITextField* textField;
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.textField = [[UITextField alloc] init];
self.textField.frame = CGRectMake(100, 100, 180, 40);
self.textField.text = @"用户名";
self.textField.font = [UIFont systemFontOfSize:15];
self.textField.textColor = [UIColor blackColor];
self.textField.borderStyle = UITextBorderStyleRoundedRect;
self.textField.keyboardType = UIKeyboardTypeDefault;
self.textField.placeholder = @"请输入用户名......";
self.textField.secureTextEntry = YES;
[self.view addSubview:self.textField];
self.textField.delegate = self;
}
- (void) textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(@"开始编辑了");
}
- (void) textFieldDidEndEditing:(UITextField *)textField
{
self.textField.text = @"";
NSLog(@"编辑即将结束");
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
return YES;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self.textField resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
心得体会
- UITextField继承于UIController,而UIController又继承于UIView,所以还是能用UIView的方法
- self.textField.text = @”用户名”是预先放在上面的文字而self.textField.placeholder = @”请输入用户名……”是以浅灰色放在上面的提示文字
- 键盘类型
- fault默认风格:26键英文键盘
- NamePhonePad字母数字组合风格
- NumberPad10个数字
- 用BOOL判断来锁死输入,使得输入规范