UITextField的使用详解

来源:互联网 发布:考上清华美院知乎 编辑:程序博客网 时间:2024/05/16 04:42

UITextField控件是开发中,使用频率比较高的控件了,那么有必要总结一下。


一、UITextField手动编写控件


UITextField *txtAccount = [[UITextFieldalloc]initWithFrame:CGRectMake(10,10,300, 30)];

    

   //设置委托

    [txtAccountsetDelegate:self];

   //设置占位符

    [txtAccountsetPlaceholder:@"账号"];

   //设置文本对齐

   [txtAccount setTextAlignment:NSTextAlignmentLeft];

   //设置样式

   [txtAccount setBorderStyle:UITextBorderStyleRoundedRect];

   //加入view

    [self.viewaddSubview: txtAccount];

    [txtAccountrelease];


二、UITextFieldDelegate委托


// 设置输入框,是否可以被修改

// NO-将无法修改,不出现键盘

//YES-可以修改,默认值 

-(BOOL)textFieldShouldBeginEditing:(UITextField*)textField{

   return YES;


}


// 当点击键盘的返回键(右下角)时,执行该方法。

// 一般用来隐藏键盘

-(BOOL)textFieldShouldReturn:(UITextField *)textField{

    if (txtAccount == textField) {

[txtAccount resignFirstResponder];

}

return YES;

}


// 当输入框获得焦点时,执行该方法。 

-(void)textFieldDidBeginEditing:(UITextField *)textField{

  NSLog(@"textFieldDidBeginEditing");


}


//指定是否允许文本字段结束编辑,允许的话,文本字段会失去firstresponder 

-(BOOL)textFieldShouldEndEditing:(UITextField*)textField{

   return YES;


}


//文本框失去firstresponder时,执行 

-(void)textFieldDidEndEditing:(UITextField *)textField{

   NSLog(@"textFieldDidEndEditing");


}


 

// 指明是否允许根据用户请求清除内容

-(BOOL)textFieldShouldClear:(UITextField *)textField{

  NSLog(@"textFieldDidEndEditing");

   return YES;

}


//文本框的文本,是否能被修改 

-(BOOL)textField:(UITextField *)textFieldshouldChangeCharactersInRange:(NSRange)rangereplacementString:(NSString*)string{

   return YES;



希望对你有所帮助!

0 0
原创粉丝点击