iOS个人整理04-UITextField文本输入框

本文详细介绍了UITextField的基本用法,包括初始化、设置属性如键盘类型、边框样式等,并演示了如何设置代理方法来监听文本框的各种状态。

UITextField--文本框


UITextField是控制文本输入和显示的控件,只能输入单行

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    

#pragma mark UITextField文本框,单行
    
    //初始化
    UITextField *myTextField = [[UITextField alloc]initWithFrame:CGRectMake(100, 350, 150, 20)];

    //占位字符,没有输入时显示的文本,点击文本框时自动清空
    myTextField.placeholder = @"请输入手机号";

    //设置键盘样式,数字键盘,字母键盘等
    myTextField.keyboardType = UIKeyboardTypeNumberPad;

    myTextField.returnKeyType = UIReturnKeySend; //设置键盘上的return键
 


//设置边框样式 
myTextField.borderStyle = UITextBorderStyleRoundedRect; 
//设置密码样式,使输入内容隐藏 
myTextField.secureTextEntry = NO; 
[self.window addSubview:Line]; 
//设置右侧小叉号,点击可以删去所有输入内容 
myTextField.clearButtonMode = UITextFieldViewModeAlways; 
//设置首字母大写 
myTextField.autocapitalizationType = UITextAutocapitalizationTypeWords; 
//通过属性控制是否可以编辑 
myTextField.enabled = YES;  
//自定义键盘 
UIView *keyBoardView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.window.frame), 256)]; 
//定义一个button放在键盘上 
UIButton *firstButton = [UIButton buttonWithType:UIButtonTypeSystem]; 
[firstButton setTitle:@"1" forState:UIControlStateNormal]; 
firstButton.frame = CGRectMake(10, 10, 50, 50); 
[keyBoardView addSubview:firstButton]; 
firstButton.backgroundColor = [UIColor whiteColor]; 
//设置点击效果  
[firstButton addTarget:self action:@selector(keyButton:) forControlEvents:UIControlEventTouchUpInside]; 
myTextField.inputView = keyBoardView; //显示 [self.window addSubview:myTextField]; 
//新建一个输入框,设置代理方法 
UITextField *delegateTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 200, 100, 100)]; 
delegateTextField.borderStyle = UITextBorderStyleLine; 
delegateTextField.placeholder = @"代理方法"; 
delegateTextField.keyboardType = UIKeyboardTypeDefault; 
//设置代理方法,代理人是本类 delegateTextField.delegate = self; 
[self.window addSubview:delegateTextField]; return YES;}
//自定义按钮的-(void)keyButton:(UIButton*)sender{ sender.selected = !sender.selected; 
UITextField *field = (UITextField*)[self.window viewWithTag:1001]; 
NSString *title = [sender titleForState:UIControlStateNormal]; 
field.text = [field.text stringByAppendingString:title];  
//回收键盘 取消第一响应者 结束编辑状态 [field resignFirstResponder]; 
//另一种方法回收键盘 //[self.view endEditing YES]; 
//变为第一响应者 [field becomeFirstResponder]; }
#pragma makr 代理方法
//是否可以开始编辑状态
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ 
   NSLog(@"%s",__func__);   
 return YES;}
//已经进入编辑状态
-(void)textFieldDidBeginEditing:(UITextField *)textField{ 
   NSLog(@"%s",__func__);
}
//是否可以结束编辑
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{  
  NSLog(@"%s",__func__);    
return YES;}
//已经结束编辑状态
-(void)textFieldDidEndEditing:(UITextField *)textField
{    
NSLog(@"%s",__func__);
}
//点击右下角return按钮所触发的代理方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField{   
 NSLog(@"return");    
//在此回收键盘   
 [textField resignFirstResponder];  
  return YES;}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{  
  //可以限制文字的改变   
 return YES;
} 


 

设置了代理方法后,UITextField的各种状态会触发代理方法,在本例中代理人设置成了self,也就是本类,然后在本类中实习代理协议要求的方法,这些方法都是@optional的,可以选择性实现

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值