iOS控件之UITextField篇
2015-06-25 16:21
477 查看
UITextField *txtAccount = [[UITextField alloc] initWithFrame:CGRectMake(10, 10,300, 30)];
// 设置委托
[txtAccount setDelegate:self];
// 设置占位符
[txtAccount setPlaceholder:@"账号"];
// 设置文本对齐
[txtAccount setTextAlignment:NSTextAlignmentLeft];
// 设置样式
[txtAccount setBorderStyle:UITextBorderStyleRoundedRect];
// 加入view中
[self.view addSubview:
txtAccount];
[txtAccount release];
二、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");
}
// 指定是否允许文本字段结束编辑,允许的话,文本字段会失去first
responder
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
return YES;
}
// 文本框失去first responder 时,执行
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"textFieldDidEndEditing");
}
// 指明是否允许根据用户请求清除内容
- (BOOL)textFieldShouldClear:(UITextField *)textField{
NSLog(@"textFieldDidEndEditing");
return YES;
}
// 文本框的文本,是否能被修改
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string{
return YES;
}
希望对你有所帮助!
// 设置委托
[txtAccount setDelegate:self];
// 设置占位符
[txtAccount setPlaceholder:@"账号"];
// 设置文本对齐
[txtAccount setTextAlignment:NSTextAlignmentLeft];
// 设置样式
[txtAccount setBorderStyle:UITextBorderStyleRoundedRect];
// 加入view中
[self.view addSubview:
txtAccount];
[txtAccount release];
二、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");
}
// 指定是否允许文本字段结束编辑,允许的话,文本字段会失去first
responder
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
return YES;
}
// 文本框失去first responder 时,执行
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"textFieldDidEndEditing");
}
// 指明是否允许根据用户请求清除内容
- (BOOL)textFieldShouldClear:(UITextField *)textField{
NSLog(@"textFieldDidEndEditing");
return YES;
}
// 文本框的文本,是否能被修改
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string{
return YES;
}
希望对你有所帮助!
相关文章推荐
- 使用NEWID()搭配UniqueIdentifier数据产生全球唯一标识码
- iOS控件之UILbel篇
- iOS学习4_UITableView的使用
- iOS学习4_UITableView的使用
- Ros(fuerte)+rgbdslam_freiburg+installation
- maven build 之 basebuild element set
- UINavigationBar 点儿事&&去掉 NavigationBar 上的分割线
- SpannableString与SpannableStringBuilder
- Unique Paths 算法详解
- Android: Failure [INSTALL_FAILED_DEXOPT] and Failure [INSTALL_FAILED_UID_CHANGED] 解决方案
- iOS 8 Auto Layout界面自动布局系列2-使用Xcode的Interface Builder添加布局约束
- salesforce csv 1000 limitation issue
- [LeetCode] Repeated DNA Sequences
- 仿QQ侧滑效果(swifit)
- unique_ptr使用简介
- IOS本地消息推送(UILocalNotification)
- getContextPath、getServletPath、getRequestURI的区别
- easyui menubutton和menu对齐
- Druid使用说明书
- UISearchbar设置placeholder字体颜色大小