UITextField 的介绍,属性以及使用

//
//  ZKViewController.m
//  01-UITextFieldDemo
//

#import "ZKViewController.h"

@interface ZKViewController ()<UITextFieldDelegate>

@end

@implementation ZKViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    //UITextField:输入框  作用:输入内容
    //创建输入框
    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 160, 40)];
    textField.center = CGPointMake(160, 120);

    //类型
    textField.borderStyle = UITextBorderStyleRoundedRect;

    //设置背景色
    textField.backgroundColor = [UIColor grayColor];

    //显示
    [self.view addSubview:textField];

    //键盘弹回
    /**
     输入框: 知道输入的内容什么时候输完,不知道键盘什么时候弹回。

     当前界面:知道键盘什么时候弹回,不知道内容什么时候输完

     当内容输完时,怎么将键盘弹回?

     通过代理

     1.在输入框的类里面写协议,协议里面有控制键盘回收等功能的方法

     2.让当前类引用协议

     3.将当前类设置成输入框的代理类

     4.在当前类里面写协议的相关方法的具体实现

     */

    textField.delegate = self;

    // Do any additional setup after loading the view.
}

//点解retun键执行的方法,作用:回收键盘
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    //第一响应项  :当前正在响应(正在与用户进行交互)的对象

    //键盘回收的做法:取消第一响应项
    [textField resignFirstResponder];

    //变成第一响应项(不常使用)
    //[textField becomeFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

//
//  ZKViewController.m
//  02-UITextField的属性
//

#import "ZKViewController.h"

typedef enum{
    UITextFieldTagWithText = 1,
    UITextFieldTagWithPass
}UITextFieldTag;

@interface ZKViewController ()<UITextFieldDelegate>

@end

@implementation ZKViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

//    self.view.backgroundColor = [UIColor redColor];

    //1.创建UITextField  输入用户名
    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 160, 40)];
    //设置中心
    textField.center = CGPointMake(160, 120);
    //设置背景颜色
    textField.backgroundColor = [UIColor grayColor];
    //设置样式
    textField.borderStyle = UITextBorderStyleRoundedRect;

    //显示
    [self.view addSubview:textField];

    textField.delegate = self;

    //1.创建UITextField  输入密码
    UITextField *passField = [[UITextField alloc]initWithFrame:CGRectMake(100, 160, 160, 40)];
    //设置输入框的tag值
    passField.tag = UITextFieldTagWithPass;
    //设置中心
    passField.center = CGPointMake(160, 180);
    //设置背景颜色
    passField.backgroundColor = [UIColor grayColor];
    //设置样式
    passField.borderStyle = UITextBorderStyleRoundedRect;

    //显示
    [self.view addSubview:passField];

    passField.delegate = self;

    //属性设置
    //1.输入前默认提示文字
    textField.placeholder = @"请输入用户名";
    passField.placeholder = @"请输入密码";

    //2添加清除按钮               总显示
    textField.clearButtonMode = UITextFieldViewModeAlways;
    passField.clearButtonMode = UITextFieldViewModeAlways;

    //3安全输入
    passField.secureTextEntry = YES;

    //4设置背景图
    //要想自定义背景图,先要将输入框的样式设置成非RoundRect
    textField.borderStyle = UITextBorderStyleLine;
    textField.background = [UIImage imageNamed:@"back"];

    passField.borderStyle = UITextBorderStyleLine;
    passField.background = [UIImage imageNamed:@"back"];

    //5限制return键的使用,当输入框没有内容的时候,return键不可用
    textField.enablesReturnKeyAutomatically = YES;
    passField.enablesReturnKeyAutomatically = YES;

    //6数字键盘
    passField.keyboardType = UIKeyboardTypeNumberPad;

    //7数字键盘回收
    //创建一个view作为inputAccessoryView
    UIView *view = [[UIView alloc]init];

    view.frame = CGRectMake(0, 0, 320, 40);

    view.backgroundColor = [UIColor grayColor];

    //在view上面添加一个按钮,用来收回键盘
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    backButton.frame = CGRectMake(10, 0, 100, 40);

    [backButton setTitle:@"收回键盘" forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(keyBoardBack:) forControlEvents:UIControlEventTouchUpInside];

    [view addSubview:backButton];

    //将view作为inputAccessoryView属性视图,可以通过inputAccessoryView给键盘添加一些额外的共嫩(按钮,标签等)
    passField.inputAccessoryView = view;

    //8.再创建一个view
//    UIView *viewInput = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 100)];
//    
//    viewInput.backgroundColor = [UIColor redColor];

    //重新设置输入框的inputView属性, 可以通过inputView属性设置自己的键盘
    //passField.inputView = viewInput;

    //9.增加输入框的左视图(文字)
    //9.1增加文字
    UILabel *leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 70, 40)];
    leftLabel.text = @"用户名:";
    //9.2增加图片
    UIImageView *imageViewLeft = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"head8_100x100"]];
    imageViewLeft.frame = CGRectMake(0, 0, 40, 40);

    //设置左视图
    textField.leftView = imageViewLeft;
    //设置显示方式
    //注意:要想让左视图显示,必须设置显示方式
    textField.leftViewMode = UITextFieldViewModeAlways;

    //10.增加右视图
    //注意:左视图与右视图同时存在的时候,必须创建两个独立的视图对象
    UIImageView *imageViewRight = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"head8_100x100"]];
    imageViewRight.frame = CGRectMake(0, 0, 40, 40);

    textField.rightView = imageViewRight;
    textField.rightViewMode = UITextFieldViewModeWhileEditing;

    //11.去掉开头字母大写提示
    textField.autocapitalizationType = UITextAutocapitalizationTypeNone;

    //12.去掉纠错提醒
    textField.autocorrectionType = UITextAutocorrectionTypeNo;

    //13.自动缩小输入的文字,进行自适应显示
    textField.adjustsFontSizeToFitWidth = YES;
    //缩小到的最小尺寸号
    textField.minimumFontSize = 5;

    //1.创建UITextField 第三个对象
    UITextField *thirdField = [[UITextField alloc]initWithFrame:CGRectMake(100, 20, 160, 80)];
    //设置中心
    thirdField.center = CGPointMake(160, 60);
    //设置背景颜色
    thirdField.backgroundColor = [UIColor grayColor];
    //设置样式
    thirdField.borderStyle = UITextBorderStyleRoundedRect;

    //显示
    [self.view addSubview:thirdField];

    //14.调整文字的位置
    //14.1上下(靠下面显示)
    thirdField.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;

    //14.2 水平(靠右边显示)
    thirdField.textAlignment = NSTextAlignmentRight;

    //15.文字的大小
    thirdField.font = [UIFont systemFontOfSize:20];

    // Do any additional setup after loading the view, typically from a nib.
}

//通过按钮方法是键盘回收
-(void)keyBoardBack:(UIButton *)button{
    UITextField *pass = (UITextField *)[self.view viewWithTag:UITextFieldTagWithPass];
    [pass resignFirstResponder];
}

//点击return键执行的方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];

    return YES;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end