鍵盤事件
[UIKeyboardWillShowNotification]
[UIKeyboardDidShowNotification]
[UIKeyboardWillHideNotification]
[UIKeyboardDidHideNotification]
使用場景:
計算鍵盤的高度瑟捣,調(diào)整UI布局
根據(jù)鍵盤顯示隱藏執(zhí)行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只可顯示不可編輯的,此時起展示作用:用此屬性設(shè)置需要顯示的文本然后設(shè)置UITextField不可交互textField.userInteractionEnabled = NO;
textField.userInteractionEnabled = NO;
B.placeholder設(shè)置字體顏色,大小
方法一:
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];
根據(jù)輸入文字動態(tài)調(diào)整字體大小,需要設(shè)置一個最小字體大小
textField.adjustsFontSizeToFitWidth = YES;
textField.minimumFontSize = 10.0;//設(shè)置最小字體
E.設(shè)置輸入內(nèi)容的對其方式
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.與鍵盤相關(guān)的屬性
textField.keyboardType = UIKeyboardTypeNumberPad; //設(shè)置鍵盤的樣式
typedef enum {
UIKeyboardTypeDefault, 默認鍵盤,支持所有字符
UIKeyboardTypeASCIICapable, 支持ASCII的默認鍵盤
UIKeyboardTypeNumbersAndPunctuation, 標準電話鍵盤,支持+*#字符
UIKeyboardTypeURL, URL鍵盤,支持.com按鈕 只支持URL字符
UIKeyboardTypeNumberPad, 數(shù)字鍵盤
UIKeyboardTypePhonePad, 電話鍵盤
UIKeyboardTypeNamePhonePad, 電話鍵盤虽界,也支持輸入人名
UIKeyboardTypeEmailAddress, 用于輸入電子 郵件地址的鍵盤
UIKeyboardTypeDecimalPad, 數(shù)字鍵盤 有數(shù)字和小數(shù)點
UIKeyboardTypeTwitter, 優(yōu)化的鍵盤,方便輸入@涛菠、#字符
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.其他相關(guān)屬性
以下幾個屬性通常不用迄薄,通常為自定義控件先設(shè)置一個UIView UIView上面再放UIImageview 和 UITextField琅关,設(shè)置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,從不出現(xiàn)
UITextFieldViewModeWhileEditing,編輯時出現(xiàn)
UITextFieldViewModeUnlessEditing,除了編輯外都出現(xiàn)
UITextFieldViewModeAlways 一直出現(xiàn)
};
textField.autocorrectionType = UITextAutocorrectionTypeNo; //是否自動糾錯
typedef enum {
UITextAutocorrectionTypeDefault, 默認
UITextAutocorrectionTypeNo, 不自動糾錯
UITextAutocorrectionTypeYes, 自動糾錯
} UITextAutocorrectionType;
secureTextEntry 啟用/禁用 UITextField對象的安全輸入功能。如果設(shè)置成YES則類似于密碼框內(nèi) 容將顯示為圓點冶伞。
autocorrectionType 啟用/禁用 UITextFieldUI想的拼寫建議功能新症,根據(jù)用戶輸入錯誤的單詞提供 修改建議
autocapitalizationType 設(shè)置 UITextField的自動大寫功能,
none:關(guān)閉大寫功能
words: 單詞
sentences: 句子
allcharacters:所有字母
四種類型