UISB TextField

viewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITextFieldDelegate>
{
    //定一个textField
    //文本输入区域
    //例如 用户名 密码需要输入文本文字的内容区域
    //只能输入单行文字 不能输入或显示更多
    UITextField* _textField;
}
@property (retain,nonatomic)UITextField* textField;


@end

viewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize textField= _textField;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //创建一个文本输入区对象
    self.textField= [[UITextField alloc]init];
    
    self.textField.frame =CGRectMake(100, 100, 80, 40);
    
    self.textField.text=@"用户名";
    
    self.textField.font=[UIFont systemFontOfSize:15];
    
    self.textField.textColor=[UIColor blackColor];
    
    //设置边框颜色
    self.textField.borderStyle=UITextBorderStyleNone;
    
    //设置虚拟键盘风格
    //UIKeyboardTypeDefault 默认风格
    //UIKeyboardTypeNamePhonePad 字母数字组合风格
    //UIKeyboardTypeNumberPad 纯数字
    self.textField.keyboardType=UIKeyboardTypeNumberPad;
    //提示文字信息
    //当text属性为空 显示此条信息
    //浅灰色提示文字
    self.textField.placeholder=@"请输入用户名...";
    //是否最为密码使用
    self.textField.secureTextEntry=NO;
    
    [self.view addSubview:self.textField];
    
    self.textField.delegate=self;
    
       
    
}
//点击屏幕空白处调用此函数
-(void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
    //是虚拟键盘回收 不作为第一响应着
    [self.textField resignFirstResponder];
    
}

-(void) textFieldDidBeginEditing:(UITextField*)textField
{
    NSLog(@"开始编辑了");
}

-(void)textFieldDidEndEditing:(UITextField*)textField
{
    self.textField.text=@"";
    NSLog(@"开始结束编辑");
    
}
//是否可以进行输入

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    return YES;
}

//是否可以结束输入 
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    return NO;
}


@end
原文地址:https://www.cnblogs.com/zhangqing979797/p/13669878.html