键盘事件
[UIKeyboardWillShowNotification]
[UIKeyboardDidShowNotification]
[UIKeyboardWillHideNotification]
[UIKeyboardDidHideNotification]
使用场景:
计算键盘的高度,调整UI布局
根据键盘显示隐藏执行UI的动画
1.使用方法
A.注册事件
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
B.接收事件
-(void)keyboardWillShow:(NSNotification)notification{
NSDictionaryinfo=[notification userInfo];
CGSize kbSize=[[infoobjectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;
NSLog(@"keyboard changed, keyboard width = %f, height = %f",
kbSize.width,kbSize.height);
}
C.NSNotification需要remove
-(void)viewDidUnload{
[superviewDidUnload];
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
2.属性(Attributes)
A.UITextField的text属性与userInteractionEnabled共用。
textField.text = @"密码输入框"
// 输入框默认输入文本,有时需求UITextField只可显示不可编辑的,此时起展示作用:用此属性设置需要显示的文本然后设置UITextField不可交互textField.userInteractionEnabled = NO;
textField.userInteractionEnabled = NO;
B.placeholder设置字体颜色,大小
方法一:
textField.placeholder = @"密码输入框"; // 提示文本
[textField setValue:[UIColor colorWithRed:79/255.0f green:79/255.0f blue:79/255.0f alpha:0.5f] forKeyPath:@"_placeholderLabel.textColor"];
[textField setValue:[UIFont boldSystemFontOfSize:20] forKeyPath:@"_placeholderLabel.font"];
方法二:
textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"密码输入框" attributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:79/255.0f green:79/255.0f blue:79/255.0f alpha:0.5f],NSFontAttributeName : [UIFont systemFontOfSize:20 weight:6],}];
C.边框
textField.borderStyle = UITextBorderStyleLine;
//效果如下图所示
typedef NS_ENUM(NSInteger, UITextBorderStyle) {
UITextBorderStyleNone,
UITextBorderStyleLine,
UITextBorderStyleBezel,
UITextBorderStyleRoundedRect
};
D.font属性
textField.font = [UIFont systemFontOfSize:14.0f];
textField.textColor = [UIColor redColor];
根据输入文字动态调整字体大小,需要设置一个最小字体大小
textField.adjustsFontSizeToFitWidth = YES;
textField.minimumFontSize = 10.0;//设置最小字体
E.设置输入内容的对其方式
textField.textAlignment = NSTextAlignmentLeft;
NSTextAlignmentLeft = 0, // Visually left aligned
NSTextAlignmentCenter = 1, // Visually centered
NSTextAlignmentRight = 2, // Visually right aligned
NSTextAlignmentRight = 1, // Visually right aligned
NSTextAlignmentCenter = 2, // Visually centered
NSTextAlignmentJustified = 3, // Fully-justified. The last line in a paragraph is natural-aligned.
NSTextAlignmentNatural = 4, // Indicates the default alignment for script
[textField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[textField setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
UIControlContentVerticalAlignmentCenter = 0,
UIControlContentVerticalAlignmentTop = 1,
UIControlContentVerticalAlignmentBottom = 2,
UIControlContentVerticalAlignmentFill = 3,
UIControlContentHorizontalAlignmentCenter = 0,
UIControlContentHorizontalAlignmentLeft = 1,
UIControlContentHorizontalAlignmentRight = 2,
UIControlContentHorizontalAlignmentFill = 3,
F.与键盘相关的属性
textField.keyboardType = UIKeyboardTypeNumberPad; //设置键盘的样式
typedef enum {
UIKeyboardTypeDefault, 默认键盘,支持所有字符
UIKeyboardTypeASCIICapable, 支持ASCII的默认键盘
UIKeyboardTypeNumbersAndPunctuation, 标准电话键盘,支持+*#字符
UIKeyboardTypeURL, URL键盘,支持.com按钮 只支持URL字符
UIKeyboardTypeNumberPad, 数字键盘
UIKeyboardTypePhonePad, 电话键盘
UIKeyboardTypeNamePhonePad, 电话键盘,也支持输入人名
UIKeyboardTypeEmailAddress, 用于输入电子 邮件地址的键盘
UIKeyboardTypeDecimalPad, 数字键盘 有数字和小数点
UIKeyboardTypeTwitter, 优化的键盘,方便输入@、#字符
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
} UIKeyboardType;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone; //首字母是否自动大写
typedef enum {
UITextAutocapitalizationTypeNone, 不自动大写
UITextAutocapitalizationTypeWords, 单词首字母大写
UITextAutocapitalizationTypeSentences, 句子的首字母大写
UITextAutocapitalizationTypeAllCharacters, 所有字母都大写
} UITextAutocapitalizationType;
textField.keyboardAppearance=UIKeyboardAppearanceDefault; //键盘外观
typedef enum {
UIKeyboardAppearanceDefault, 默认外观,浅灰色
UIKeyboardAppearanceAlert, 深灰 石墨色
} UIReturnKeyType;
textField.returnKeyType =UIReturnKeyDone; //return键变成什么键
typedef enum {
UIReturnKeyDefault, 默认 灰色按钮,标有Return
UIReturnKeyGo, 标有Go的蓝色按钮
UIReturnKeyGoogle, 标有Google的蓝色按钮,用语搜索
UIReturnKeyJoin, 标有Join的蓝色按钮
UIReturnKeyNext, 标有Next的蓝色按钮
UIReturnKeyRoute, 标有Route的蓝色按钮
UIReturnKeySearch, 标有Search的蓝色按钮
UIReturnKeySend, 标有Send的蓝色按钮
UIReturnKeyYahoo, 标有Yahoo的蓝色按钮
UIReturnKeyYahoo, 标有Yahoo的蓝色按钮
UIReturnKeyEmergencyCall, 紧急呼叫按钮
} UIReturnKeyType;
G.其他相关属性
以下几个属性通常不用,通常为自定义控件先设置一个UIView UIView上面再放UIImageview 和 UITextField,设置UIView的boder属性以满足要求。
leftView
Property
leftViewMode
Property
rightView
Property
rightViewMode
Property
清空输入框,常见于密码输入错入重新输入时会清空输入框。
textField.clearButtonMode = UITextFieldViewModeWhileEditing;// sets when the clear button shows up编辑的时候清空输入框
clearsOnInsertion// whether inserting text replaces the previous contents.插入的时候清空输入框
typedef NS_ENUM(NSInteger, UITextFieldViewMode) {
UITextFieldViewModeNever,从不出现
UITextFieldViewModeWhileEditing,编辑时出现
UITextFieldViewModeUnlessEditing,除了编辑外都出现
UITextFieldViewModeAlways 一直出现
};
textField.autocorrectionType = UITextAutocorrectionTypeNo; //是否自动纠错
typedef enum {
UITextAutocorrectionTypeDefault, 默认
UITextAutocorrectionTypeNo, 不自动纠错
UITextAutocorrectionTypeYes, 自动纠错
} UITextAutocorrectionType;
secureTextEntry 启用/禁用 UITextField对象的安全输入功能。如果设置成YES则类似于密码框内 容将显示为圆点。
autocorrectionType 启用/禁用 UITextFieldUI想的拼写建议功能,根据用户输入错误的单词提供 修改建议
autocapitalizationType 设置 UITextField的自动大写功能,
none:关闭大写功能
words: 单词
sentences: 句子
allcharacters:所有字母
四种类型