iOS开发系列之三 - UITextField 使用方法小结
// 初始化输入框并设置位置和大小
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 100, 300, 30)];
// 设置输入框提示
textField.placeholder = @"TextField Tip";
// 输入框中预先输入的文字
textField.text = @"预先输入的文字";
// 设置输入框文本的字体
textField.font = [UIFont fontWithName:@"Arial" size:20.0f];
// 设置输入框字体颜色
textField.textColor = [UIColor redColor];
// 设置输入框的背景颜色
textField.backgroundColor = [UIColor grayColor];
// 设置输入框边框样式
textField.borderStyle = UITextBorderStyleRoundedRect;
// 边框样式有下面几种:
// enum {
// UITextBorderStyleNone, 无边框。默认
// UITextBorderStyleLine, 有线型边框
// UITextBorderStyleBezel, 有线型边框和阴影
// UITextBorderStyleRoundedRect 有圆角边框
// } UITextBorderStyle;
// 设置输入框是否用于password
textField.secureTextEntry = NO;
// 设置是否有清除button。在什么时候显示。用于一次性删除输入框中的全部内容
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
// 清除button样式有下面几种:
// enum {
// UITextFieldViewModeNever, 从不出现
// UITextFieldViewModeWhileEditing, 编辑时出现
// UITextFieldViewModeUnlessEditing, 除了编辑外都出现
// UITextFieldViewModeAlways 一直出现
// } UITextFieldViewMode;
// 设置自己主动纠错方式
textField.autocorrectionType = UITextAutocorrectionTypeNo;
// 自己主动纠错方式有下面几种:
// enum {
// UITextAutocorrectionTypeDefault, 默认
// UITextAutocorrectionTypeNo, 不自己主动纠错
// UITextAutocorrectionTypeYes, 自己主动纠错
// } UITextAutocorrectionType;
// 设置自己主动大写方式
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
// 自己主动大写方式有下面几种:
// enum {
// UITextAutocapitalizationTypeNone, 不自己主动大写
// UITextAutocapitalizationTypeWords, 单词首字母大写
// UITextAutocapitalizationTypeSentences, 句子的首字母大写
// UITextAutocapitalizationTypeAllCharacters, 全部字母都大写
// } UITextAutocapitalizationType;
// 设置再次编辑是否清空
textField.clearsOnBeginEditing = YES;
// 设置文本对齐方式
textField.textAlignment = NSTextAlignmentLeft;
// iOS7中文本对齐方式有下面几种:
// enum {
// NSTextAlignmentLeft = 0, 左对齐。默认
// NSTextAlignmentCenter = 1, 居中对齐
// NSTextAlignmentRight = 2, 右对齐
// NSTextAlignmentJustified = 3, 在一个段落的最后一行自然对齐
// NSTextAlignmentNatural = 4, 默认对齐方式
// } NSTextAlignment;
// 设置字体大小是否自己主动适应输入框宽度。默认是保持原来大小。长文本滚动
textField.adjustsFontSizeToFitWidth = YES;
// 设置自己主动缩小显示的最小字体大小
textField.minimumFontSize = 20;
// 设置键盘的样式
textField.keyboardType = UIKeyboardTypeNumberPad;
// 键盘样式有下面几种:
// enum {
// UIKeyboardTypeDefault, 默认键盘。支持全部字符
// UIKeyboardTypeASCIICapable, 支持ASCII的默认键盘
// UIKeyboardTypeNumbersAndPunctuation, 标准电话键盘,支持+*#字符
// UIKeyboardTypeURL, 仅仅支持URL字符的URL键盘,支持.combutton
// UIKeyboardTypeNumberPad, 数字键盘
// UIKeyboardTypePhonePad, 电话键盘
// UIKeyboardTypeNamePhonePad, 支持输入人名的电话键盘
// UIKeyboardTypeEmailAddress, 电子邮件键盘
// UIKeyboardTypeDecimalPad, 有数字和小数点的数字键盘
// UIKeyboardTypeTwitter, 优化的键盘。方便输入@、#字符
// UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
// } UIKeyboardType;
// 设置return键样式
textField.returnKeyType = UIReturnKeyDone;
// return键有下面几种样式:
// enum {
// UIReturnKeyDefault, 默认,灰色button。标有Return
// UIReturnKeyGo, 标有Go的蓝色button
// UIReturnKeyGoogle, 标有Google的蓝色button,用于搜索
// UIReturnKeyJoin, 标有Join的蓝色button
// UIReturnKeyNext, 标有Next的蓝色button
// UIReturnKeyRoute, 标有Route的蓝色button
// UIReturnKeySearch, 标有Search的蓝色button
// UIReturnKeySend, 标有Send的蓝色button
// UIReturnKeyYahoo, 标有Yahoo的蓝色button
// UIReturnKeyYahoo, 标有Yahoo的蓝色button
// UIReturnKeyEmergencyCall, 紧急呼叫button
// } UIReturnKeyType;
// 设置键盘外观
textField.keyboardAppearance = UIKeyboardAppearanceDefault;
// 键盘外观有一下两种:
// enum {
// UIKeyboardAppearanceDefault, 默认外观,浅灰色
// UIKeyboardAppearanceAlert。 深灰,石墨色
// } UIReturnKeyType;
// 设置代理,用于实现协议
textField.delegate = self;
// 最右側加图片是下面代码,左側相似
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];
textField.rightView = image;
textField.rightViewMode = UITextFieldViewModeAlways;
// 把输入框加到视图中
[self.view addSubview:textField];
// 按return键收起键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[text resignFirstResponder];
return YES;
}
本文固定链接:http://www.itechzero.com/ios-development-series-three-uitextfield-usage-summary.html。转载请注明出处。
浙公网安备 33010602011771号