#import "ViewController.h"
@interface ViewController ()<UITextFieldDelegate>
@property (nonatomic,strong) UITextField *textfield;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//初始化
UITextField *textfield = [[UITextField alloc]initWithFrame:CGRectMake(50,100, 200, 43)];
[self.view addSubview: textfield];
//占位符
textfield.placeholder = @"请输入";
//设置text的边框样式(圆角)
textfield.borderStyle = UITextBorderStyleRoundedRect;
//Textfield 显示字体的颜色
textfield.textColor = [UIColor redColor];
//占位符设置
textfield.tintColor = [UIColor greenColor];
//设置Textfield左侧view的显示模式
textfield.leftViewMode = UITextFieldViewModeAlways;
//在Textfield左侧添加图片
UIImageView *leftImgV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
leftImgV.image = [UIImage imageNamed:@"notlook.png"];
textfield.leftView = leftImgV;
//文字显示的方式默认左侧对齐
textfield.textAlignment = NSTextAlignmentCenter;
//密文输入
textfield.secureTextEntry = YES;
//再次输入情况Textfield
textfield.clearsOnBeginEditing = YES;
//键盘样式
textfield.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
//代理方法
textfield.delegate = self;
//扩充textfield的生命周期
self.textfield = textfield;
}
#pragma mark ----Textfield代理方法----
//回收键盘
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
//点击空白处回收键盘
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self.textfield resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end