//
// ViewController.m
// TextFieldAll
//
#import "ViewController.h"
@interface ViewController ()<UITextFieldDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UITextField *myTextField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 100, 50)];
myTextField.backgroundColor = [UIColor brownColor];
//设置边框样式,只有设置了才会显示边框样式
myTextField.borderStyle =UITextBorderStyleBezel;
/*
UITextBorderStyleNone, 默认的
UITextBorderStyleLine,
UITextBorderStyleBezel,
UITextBorderStyleRoundedRect 圆角
*/
//设置边框宽度,圆角,颜色
myTextField.layer.borderWidth = 2.0f;
myTextField.layer.cornerRadius = 5;
myTextField.layer.borderColor = [UIColor colorWithRed:14/255.0 green:174/255.0 blue:131/255.0 alpha:1].CGColor;
//设置背景//UITextField 的背景,注意只有UITextBorderStyleNone的时候改属性有效
myTextField.background = [UIImage imageNamed:@"dd.png"];
//设置enable为no时,textfield的背景
myTextField.disabledBackground = [UIImage imageNamed:@"cc.png"];
//placeholder
myTextField.placeholder =@"password";
//设置输入框内容的字体样式和大小
myTextField.font = [UIFont fontWithName:@"Arial" size:20.0f];
//设置文字的颜色
myTextField.textColor = [UIColor redColor];
//输入框中是否有个叉号,在什么时候显示,用于一次性删除输入框中的内容
myTextField.clearButtonMode = UITextFieldViewModeAlways;
/*
UITextFieldViewModeNever, 重不出现
UITextFieldViewModeWhileEditing, 编辑时出现
UITextFieldViewModeUnlessEditing, 除了编辑外都出现
UITextFieldViewModeAlways 一直出现
*/
//占位字符
myTextField.text = @"一开始就在输入框的文字";
//每输入一个字符就变成点 用来输入密码时
myTextField.secureTextEntry = YES;
//当文本小部件具有零长度内容时,将自动禁用返回键,当文本小部件具有非零长度内容时将自动启用
myTextField.enablesReturnKeyAutomatically = YES;
// 是否纠错
myTextField.autocorrectionType =UITextAutocorrectionTypeNo;
/*
UITextAutocorrectionTypeDefault, 默认
UITextAutocorrectionTypeNo, 不自动纠错
UITextAutocorrectionTypeYes, 自动纠错
*/
//再次编辑就清空
myTextField.clearsOnBeginEditing = YES;
//内容对齐方式
myTextField.textAlignment = NSTextAlignmentLeft;
//内容的垂直对齐方式 UITextField继承自UIControl,此类中有一个属性contentVerticalAlignment
myTextField.contentVerticalAlignment =UIControlContentVerticalAlignmentCenter;
//设置为YES时文本会自动缩小以适应文本窗口大小.默认是保持原来大小,而让长文本滚动
myTextField.adjustsFontSizeToFitWidth =YES;
//设置自动缩小显示的最小字体大小
myTextField.minimumFontSize = 20;
//设置键盘的样式
myTextField.keyboardType =UIKeyboardTypeNumberPad;
/*
UIKeyboardTypeDefault, 默认键盘,支持所有字符
UIKeyboardTypeASCIICapable, 支持ASCII的默认键盘
UIKeyboardTypeNumbersAndPunctuation, 标准电话键盘,支持+*#字符
UIKeyboardTypeURL, URL键盘,支持.com按钮 只支持URL字符
UIKeyboardTypeNumberPad, 数字键盘
UIKeyboardTypePhonePad, 电话键盘
UIKeyboardTypeNamePhonePad, 电话键盘,也支持输入人名
UIKeyboardTypeEmailAddress, 用于输入电子 邮件地址的键盘
UIKeyboardTypeDecimalPad, 数字键盘 有数字和小数点
UIKeyboardTypeTwitter, 优化的键盘,方便输入@、#字符
UIKeyboardTypeAlphabet =UIKeyboardTypeASCIICapable,
*/
//首字母是否大写
myTextField.autocapitalizationType =UITextAutocapitalizationTypeNone;
/*
UITextAutocapitalizationTypeNone, 不自动大写
UITextAutocapitalizationTypeWords, 单词首字母大写
UITextAutocapitalizationTypeSentences, 句子的首字母大写
UITextAutocapitalizationTypeAllCharacters, 所有字母都大写
*/
//return键变成什么键
myTextField.returnKeyType =UIReturnKeyDone;
/*
UIReturnKeyDefault, 默认 灰色按钮,标有Return
UIReturnKeyGo, 标有Go的蓝色按钮
UIReturnKeyGoogle,标有Google的蓝色按钮,用语搜索
UIReturnKeyJoin,标有Join的蓝色按钮
UIReturnKeyNext,标有Next的蓝色按钮
UIReturnKeyRoute,标有Route的蓝色按钮
UIReturnKeySearch,标有Search的蓝色按钮
UIReturnKeySend,标有Send的蓝色按钮
UIReturnKeyYahoo,标有Yahoo的蓝色按钮
UIReturnKeyYahoo,标有Yahoo的蓝色按钮
UIReturnKeyEmergencyCall, 紧急呼叫按钮
*/
//键盘外观
myTextField.keyboardAppearance=UIKeyboardAppearanceAlert;
/*
UIKeyboardAppearanceDefault, 默认外观,浅灰色
UIKeyboardAppearanceAlert, 深灰 石墨色
*/
myTextField.delegate = self;
[self.view addSubview:myTextField];
}
/*
UIKeyboardWillShowNotification //键盘显示之前发送
UIKeyboardDidShowNotification //键盘显示之后发送
UIKeyboardWillHideNotification //键盘隐藏之前发送
UIKeyboardDidHideNotification //键盘隐藏之后发送
*/
#pragma mark 代理方法
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
//返回一个BOOL值,YES代表允许编辑,NO不允许编辑.
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
//返回BOOL值,指定是否允许文本字段结束编辑,当编辑结束,文本字段会让出first responder
//要想在用户结束编辑时阻止文本字段消失,可以返回NO ,返回NO,点击键盘的返回按钮会无效果。
return NO;
}
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
return YES;
}
//文本框已经进入编辑模式
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
//开始编辑时触发
}
//文本框是否可以结束编辑模式
-(BOOL)textFieldShowEndEditing:(UITextField *)textField
{
//返回NO,无法结束编辑状态
return YES;
}
//文本框已结束编辑模式
-(void)textFieldDidEndEditing:(UITextField *)textField
{
}
//是否可以点击clear按钮
-(BOOL)textFieldShouldClear:(UITextField *)textField
{
//返回NO,点击clear按钮无响应
return YES;
}
//是否可以点击return按钮
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
//返回一个BOOL值,指明是否允许在按下回车键时结束编辑
//如果允许要调用resignFirstResponder 方法
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end