Objective-C
// 设置边框样式
/*
UITextBorderStyleNone, 无边框,默认
UITextBorderStyleLine, 直线边框
UITextBorderStyleBezel, 边框 + 阴影
UITextBorderStyleRoundedRect 圆角矩形边框
*/
textField.borderStyle = UITextBorderStyleLine;
// 设置背景颜色
/*
默认是透明的
*/
textField.backgroundColor = [UIColor yellowColor];
// 设置背景图片
textField.background = [UIImage imageNamed:@"pic2"];
// 设置提示文字
/*
用户输入时自动消失
*/
textField.placeholder = @"请输入用户名";
// 设置输入的字体颜色
textField.textColor = [UIColor redColor];
// 设置文字对齐方式
textField.textAlignment = NSTextAlignmentLeft;
// 设置最小可缩小的字号
textField.minimumFontSize = 10;
// 自动调整文字大小
/*
自动调整文字的大小以适应 textField 的宽度
*/
textField.adjustsFontSizeToFitWidth = YES;
// 设置密文输入模式
/*
default is NO
*/
textField.secureTextEntry = YES;
// 设置显示清除按钮
/*
UITextFieldViewModeNever, // default
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways
*/
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
// 设置键盘样式
/*
UIKeyboardTypeDefault, // Default type for the current input method.
UIKeyboardTypeASCIICapable, // Displays a keyboard which can enter ASCII characters,
// non-ASCII keyboards remain active
UIKeyboardTypeNumbersAndPunctuation, // Numbers and assorted punctuation.
UIKeyboardTypeURL, // A type optimized for URL entry.
UIKeyboardTypeNumberPad, // A number pad (0-9). Suitable for PIN entry.
UIKeyboardTypePhonePad, // A phone pad (1-9, *, 0, #, with letters under the numbers).
UIKeyboardTypeNamePhonePad, // A type optimized for entering a person's name or phone number.
UIKeyboardTypeEmailAddress, // A type optimized for multiple email address entry.
UIKeyboardTypeDecimalPad, // A number pad with a decimal point.
UIKeyboardTypeTwitter, // A type optimized for twitter text entry (easy access to @ #)
UIKeyboardTypeWebSearch, // A default keyboard type with URL-oriented addition.
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated
*/
textField.keyboardType = UIKeyboardTypeDefault;
// 设置返回键样式
/*
UIReturnKeyDefault,
UIReturnKeyGo,
UIReturnKeyGoogle,
UIReturnKeyJoin,
UIReturnKeyNext,
UIReturnKeyRoute,
UIReturnKeySearch,
UIReturnKeySend,
UIReturnKeyYahoo,
UIReturnKeyDone,
UIReturnKeyEmergencyCall,
UIReturnKeyContinue NS_ENUM_AVAILABLE_IOS(9_0),
*/
textField.returnKeyType = UIReturnKeyJoin;
// 设置输入的字母大小写模式
/*
UITextAutocapitalizationTypeNone,
UITextAutocapitalizationTypeWords,
UITextAutocapitalizationTypeSentences,
UITextAutocapitalizationTypeAllCharacters,
*/
textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
// 设置左右视图显示模式
/*
不设置模式,左右视图显示不出来
UITextFieldViewModeNever,
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways
*/
textField.leftViewMode = UITextFieldViewModeAlways;
textField.rightViewMode = UITextFieldViewModeAlways;
// 设置左右视图
textField.leftView = label1;
textField.rightView = label2;
// 让 textField 获取第一响应
/*
打开应用程序或界面时直接弹出键盘
*/
[textField becomeFirstResponder];
// 让 textField 放弃第一响应
/*
收起键盘
*/
[textField resignFirstResponder];
// 设置 textField 的代理,需遵守协议 <UITextFieldDelegate>
textField.delegate = self;