//
// ViewController.h
//
//
// Created by pengjiaxin on 2017/10/19.
// Copyright © 2017年 pengjiaxin. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
//文本输入区域
//只能输入单行文字,不能输入或显示多行
//成员变量
UITextField *_textField;
}
/**
属性
*/
@property (nonatomic, strong) UITextField *textField;
@end
//
// ViewController.m
//
//
// Created by pengjiaxin on 2017/10/19.
// Copyright © 2017年 pengjiaxin. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<UITextFieldDelegate>
@end
@implementation ViewController
//同步---
@synthesize textField = _textField;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.textField = [[UITextField alloc]init];
_textField.frame = CGRectMake(100, 100, 180, 40);
_textField.textColor = [UIColor redColor];
_textField.font = [UIFont systemFontOfSize:15];
//边框风格:圆角风格/线框风格/bezel线框/None
_textField.borderStyle = UITextBorderStyleRoundedRect;
/*
UIKeyboardTypeDefault:默认风格
UIKeyboardTypeNamePhonePad:字母和数字风格
UIKeyboardTypeNumberPad:纯数字风格
*/
_textField.keyboardType = UIKeyboardTypeNumberPad;
_textField.placeholder = @"请输入";
//是否作为密码输入
//YES:密码输入
//NO:正常显示输入的文字
_textField.secureTextEntry = NO;
_textField.delegate = self;
[self.view addSubview:_textField];
}
/**
点击空白处调用此方法
*/
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//使 虚拟键盘回收,不在作为第一响应者
[self.textField resignFirstResponder];
}
#pragma mark UITextField delegate
-(void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"开始编辑了");
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
//结束编辑时清空之前输入的内容
self.textField.text = @"";
NSLog(@"编辑结束");
}
/**
是否可以进行输入
YES:可以进行输入,默认为YES
NO:不能输入文字
*/
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return YES;
}
/**
是否可以结束输入
YES:可以结束输入,默认为YES
NO:不能结束输入
*/
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end