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;

// 设置输入框是否用于密码
textField.secureTextEntry = NO;
// 设置是否有清除按钮,在什么时候显示,用于一次性删除输入框中的所有内容
textField.clearButtonMode = UITextFieldViewModeWhileEditing;

// 清除按钮样式有以下几种:
//    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键盘,支持.com按钮
//        UIKeyboardTypeNumberPad,              数字键盘
//        UIKeyboardTypePhonePad,               电话键盘
//        UIKeyboardTypeNamePhonePad,           支持输入人名的电话键盘
//        UIKeyboardTypeEmailAddress,           电子邮件键盘
//        UIKeyboardTypeDecimalPad,             有数字和小数点的数字键盘
//        UIKeyboardTypeTwitter,                优化的键盘,方便输入@、#字符
//        UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
//    } UIKeyboardType;

// 设置return键样式
textField.returnKeyType = UIReturnKeyDone;

// return键有以下几种样式:
//    enum {
//        UIReturnKeyDefault,        默认,灰色按钮,标有Return
//        UIReturnKeyGo,             标有Go的蓝色按钮
//        UIReturnKeyGoogle,         标有Google的蓝色按钮,用于搜索
//        UIReturnKeyJoin,           标有Join的蓝色按钮
//        UIReturnKeyNext,           标有Next的蓝色按钮
//        UIReturnKeyRoute,          标有Route的蓝色按钮
//        UIReturnKeySearch,         标有Search的蓝色按钮
//        UIReturnKeySend,           标有Send的蓝色按钮
//        UIReturnKeyYahoo,          标有Yahoo的蓝色按钮
//        UIReturnKeyYahoo,          标有Yahoo的蓝色按钮
//        UIReturnKeyEmergencyCall,  紧急呼叫按钮
//    } 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;
}

暂无评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

验证码已失效,请刷新验证码