[API] UITextField

2017-08-28  本文已影响12人  zhulang424

单行可编辑文本区域

创建

CGRect textFieldFrame = CGRectMake(0, [UIApplication sharedApplication].statusBarFrame.size.height, 100, 50);
UITextField *textField = [[UITextField alloc] initWithFrame:textFieldFrame];

设置外观

textField.backgroundColor = [UIColor whiteColor];

// UITextBorderStyleNone : 只有在该样式下,设置背景图才会生效,且图片大小小于 textField 的frame时,图片会拉伸。
// UITextBorderStyleLine
// UITextBorderStyleBezel
// UITextBorderStyleRoundedRect
textField.backgroundColor = [UIColor whiteColor];
textField.borderStyle = UITextBorderStyleNone;
textField.background = [UIImage imageNamed:@"xxx.png"];
textField.disabledBackground = [UIImage imageNamed:@"xxxx.png"];
self.textField.leftView = leftOverlayButton;(rightView 同理)
self.textField.leftViewMode = UITextFieldViewModeAlways;

// UITextFieldViewModeNever
// UITextFieldViewModeWhileEditing
// UITextFieldViewModeUnlessEditing
// UITextFieldViewModeAlways

// clearButton 占据 rightView
// 所以当 rightView 和 clearButton 都有时,需要设置不同的 viewMode 防止显示冲突
textField.clearButtonMode = UITextFieldViewModeWhileEditing;

设置文本

textField.text = @"123";
textField.attributedText = [[NSAttributedString alloc] initWithString:@"123" 
                                                           attributes:@{NSForegroundColorAttributeName : [UIColor blackColor]}];
textField.placeholder = @"placeHolder";
textField.attributedPlaceholder  = [[NSAttributedString alloc] initWithString:@"placeholder"
                                                                   attributes:@{NSForegroundColorAttributeName : [UIColor grayColor]}];

格式化输入(属性 / 富文本)

富文本方式格式化
// 原始文本特性
textField.defaultTextAttributes = @{NSForegroundColorAttributeName : [UIColor blackColor]};
// 新输入文本特性
textField.typingAttributes = @{NSForegroundColorAttributeName : [UIColor blackColor]};
属性方式格式化
textField.font = [UIFont systemFontOfSize:13.0];
textField.textColor = [UIColor grayColor];
textField.textAlignment = NSTextAlignmentLeft;

// NSTextAlignmentLeft
// NSTextAlignmentCenter
// NSTextAlignmentRight
// NSTextAlignmentJustified
// NSTextAlignmentNatural
textField.secureTextEntry = NO;
textFiled.adjustsFontSizeToFitWidth = YES;
//设置文本最小字体,adjustsFontSizeToFitWidth == YES 时才起作用(默认0.0)
textFiled.minimumFontSize = 10;

管理输入行为(属性)

键盘

textField.inputView = nil; // 设置成为 first responder 时显示的 View
- becomeFirstResponder
- resignFirstResponder
// already become first responder and began editing
[UIKeyboardWillShowNotification]
[UIKeyboardDidShowNotification]
[UIKeyboardWillChangeFrameNotification] (keyboard was already visible)
[UIKeyboardDidChangeFrameNotification] (keyboard was already visible)

// already resign first responder already and end editing
[UIKeyboardWillHideNotification]
[UIKeyboardDidHideNotification]
textField.keyboardType = UIKeyboardTypeDefault;

常用
// UIKeyboardTypeDefault : 默认键盘,支持所有字符
// UIKeyboardTypePhonePad : 电话键盘
// UIKeyboardTypeNumberPad : 数字键盘
// UIKeyboardTypeEmailAddress : 邮件地址键盘

非常用
// UIKeyboardTypeASCIICapable : 支持ASCII的默认键盘
// UIKeyboardTypeNumbersAndPunctuation : 标准电话键盘,支持+*#字符
// UIKeyboardTypeURL : URL键盘,支持.com按钮 只支持URL字符
// UIKeyboardTypeNamePhonePad : 电话键盘,也支持输入人名
// UIKeyboardTypeDecimalPad : 数字键盘 有数字和小数点(IOS 4.1后可用)
// UIKeyboardTypeTwitter : 优化键盘,方便输入@、#字符,用于twitter文本输入(IOS 5.0后可用)
// UIKeyboardTypeWebSearch  : 支持添加链接的默认键盘类型(IOS 7.0之后可用)
// UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable : 弃用
textField.returnKeyType = UIReturnKeyDefault;

常用
// UIReturnKeyDefault : 灰色按钮[Return]
// UIReturnKeyDone : 蓝色按钮[Done]
// UIReturnKeySearch : 蓝色按钮[Search]
// UIReturnKeySend : 蓝色按钮[Send]
// UIReturnKeyJoin : 蓝色按钮[Join]
// UIReturnKeyNext : 蓝色按钮[Next]
// UIReturnKeyGo : 蓝色按钮[Go]

非常用
// UIReturnKeyGoogle : 蓝色按钮[Google](用于搜索)
// UIReturnKeyRoute : 蓝色按钮[Route]
// UIReturnKeyYahoo : 蓝色按钮[Yahoo]
// UIReturnKeyEmergencyCall : 紧急呼叫按钮
// UIReturnKeyContinue : 蓝色按钮[Continue](IOS 9.0后可用)
textField.keyboardAppearance = UIKeyboardAppearanceDefault;

// UIKeyboardAppearanceDefault : 默认
// UIKeyboardAppearanceDark : 亮(IOS 7.0后可用)
// UIKeyboardAppearanceLight  : 暗(IOS 7.0后可用)
// UIKeyboardAppearanceAlert == UIKeyboardAppearanceDark : 弃用
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;

// UITextAutocapitalizationTypeNone : 关闭
// UITextAutocapitalizationTypeWords : 单词
// UITextAutocapitalizationTypeSentences : 句子
// UITextAutocapitalizationTypeAllCharacters : 所有字母
textField.autocorrectionType = UITextAutocorrectionTypeNo;

// UITextAutocorrectionTypeDefault
// UITextAutocorrectionTypeNo
// UITextAutocorrectionTypeYes
textField.spellCheckingType = UITextSpellCheckingTypeDefault;

// UITextSpellCheckingTypeDefault
// UITextSpellCheckingTypeNo
// UITextSpellCheckingTypeYes
// 默认是 NO ,换行键一直可用
// 设置为 YES ,没有输入文本时换行键被禁用
textField.enablesReturnKeyAutomatically = YES;

Delegate

确认文本和管理输入过程

具体方法
// 是否允许开始编辑
method : - textFieldShouldBeginEditing:
// 显示键盘
notification : 
[UIKeyboardWillShowNotification]
[UIKeyboardDidShowNotification]
[UIKeyboardWillChangeFrameNotification] (keyboard was already visible)
[UIKeyboardDidChangeFrameNotification] (keyboard was already visible)

// 开始编辑
method : - textFieldDidBeginEditing
notification : [UITextFieldTextDidBeginEditingNotification]

// 编辑中
// 是否允许改变文本
method : - textField:shouldChangeCharactersInRange:replacementString:
notification : [UITextFieldTextDidChangeNotification]
// 是否允许结束编辑
method : - textFieldShouldEndEditing:
// 隐藏键盘
notification : 
[UIKeyboardWillHideNotification]
[UIKeyboardDidHideNotification]

// end editing
method : - textFieldDidEndEditing:
notification : [UITextFieldTextDidEndEditingNotification]
method : - textFieldShouldClear:
method : - textFieldShouldReturn:

Notification

限制只能输入特定字符

(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
// 把可用字符放入 Set,取反
//invertedSet方法是去反字符,把所有的除了NUMBERS 里的字符都找出来(包含去空格功能)
NSCharacterSet *validCharactersSet = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet];;

// 用 Set 中字符去拆分输入字符串并用""合并
NSString *filteredString = [[string componentsSeparatedByCharactersInSet: validCharactersSet]componentsJoinedByString:@""]; 

// 验证与原字符串是否一样,一样的话通过,不一样禁止
BOOL canChange = [string isEqualToString: filteredString];

return canChange;
}

#define NUMBERS @”0123456789n” (这个代表可以输入数字和换行,请注意这个n,如果不写这个,Done按键将不会触发,如果用在SearchBar中,将会不触发Search事件)
#define kAlphaNum   @”ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789″。

限制输入文本长度

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; 
{ 
// textField 是此时正在输入的那个输入框
// string 是输入的字符串
// range 是输入字符串范围

// 允许按回车
if ([string isEqualToString:@"n"]) 
{ 
    return YES; 
} 
// 文本长度超过20后,禁止输入
if (self.myTextField == textField) 
{ 
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; // 编辑后的文本
    if ([toBeString length] > 20) { 
        textField.text = [toBeString substringToIndex:20]; 
        return NO; 
    } 
} 
return YES; 
}
上一篇 下一篇

猜你喜欢

热点阅读