一、初始化並設置位置及大小
UITextField
*text = [[UITextField
alloc]initWithFrame:CGRectMake(20,
20,
130,
30)];
[self.view
addSubview:text];
二、UITextField 的常用屬性
(1)設置樣式
1、borderStyle - 設置邊框樣式
text.borderStyle
=
UITextBorderStyleRoundedRect;
- UITextBorderStyleNone - 無邊框
- UITextBorderStyleLine - 有邊框,直線
- UITextBorderStyleBezel - 有邊框和陰影(上邊框和左邊框加重)
- UITextBorderStyleRoundedRect - 圓角
2、 backgroundColor - 設置輸入框的背景顏色
text.backgroundColor
= [UIColor
whiteColor];
3、cornerRadius - 設置圓角
field.layer.cornerRadius=5;
field.layer.masksToBounds
=
YES;
(2)設置圖片
1、background - 設置背景圖片
text.background
= [UIImage
imageNamed:@"11.png"];
- 注意:當 UITextBorderStyle 的值爲
UITextBorderStyleRoundedRect 時,無法設置
- 跟邊框樣式有衝突
2、disabledBackground - 設置在不可用的狀態下的背景圖片
text.disabledBackground
= [UIImage
imageNamed:@"cc.png"];
注意:如果 background 的值沒有設置,則會讓 disableBackground 的值失效
3、
rightView / leftView - 設置最右側/左側加圖片
最右側加圖片的代碼:(左側類似)
UIImageView
*image=[[UIImageView
alloc]
initWithImage:[UIImage
imageNamed:@"right.png"]];
text.rightView=image;
4、rightViewMode /
leftViewMode - 設置圖片的顯示類型
最右側圖片顯示類型的代碼:(左側類似)
text.rightViewMode
=
UITextFieldViewModeAlways;
UITextFieldViewModeNever -
UITextFieldViewModeWhileEditing - 編輯模式
UITextFieldViewModeUnlessEditing - 非編輯模式
UITextFieldViewModeAlways -
(3)設置文字
1、text - 設置輸入框一開始就有的文字
2、placeholder -
默認值
text.placeholder=@"輸入密碼";
- 可以在文本框中顯示灰色的字,用於提示用戶應該在這個文本框輸入什麼內容。當這個文本框中輸入了數據時,用於提示的灰色的字將會自動消失。
3、font - 設置輸入字體
text.font
= [UIFont
fontWithName:@"Arial"
size:20.0f];
4、textColor
- 設置字體顏色
text.textColor
= [UIColor
redColor];
5、placeholder - 設置輸入框內默認的文字
text.placeholder
=
@"password";
-
當輸入框沒有內容時,水印提示提示內容爲password
6、textAlignment - 設置輸入框內內容的對齊方式
text.textAlignment
=
NSTextAlignmentRight;
- NSTextAlignmentLeft - 左對齊,默認
- NSTextAlignmentCenter - 居中對齊
- NSTextAlignmentRight - 右對齊
- NSTextAlignmentJustified - 在一個段落的最後一行自然對齊
- NSTextAlignmentNatural - 默認對齊方式
7、
contentVerticalAlignment - 設置內容的垂直對齊方式
text.contentVerticalAlignment
=
UIControlContentVerticalAlignmentCenter;
-
UIControlContentVerticalAlignmentCenter - 居中對齊
- UIControlContentVerticalAlignmentTop - 頂部對齊
- UIControlContentVerticalAlignmentBottom - 底部對齊
- UIControlContentVerticalAlignmentFill - 填充
8、
contentHorizontalAlignment - 水平對齊方式
UIControlContentHorizontalAlignmentCenter - 居中
UIControlContentHorizontalAlignmentLeft - 頂部
UIControlContentHorizontalAlignmentRight 底部
UIControlContentHorizontalAlignmentFill - 填充
9、
secureTextEntry - 每輸入一個字符就變成點,用於密碼輸入
text.secureTextEntry
=
YES;
10、autocorrectionType - 是否糾錯
text.autocorrectionType
=
UITextAutocorrectionTypeNo;
- UITextAutocorrectionTypeDefault - 默認
- UITextAutocorrectionTypeNo - 不自動糾錯
- UITextAutocorrectionTypeYes - 自動糾錯
11、clearsOnBeginEditing - 設置再次編輯是否清空
text.clearsOnBeginEditing
=
YES;
12、
minimumFontSize - 設置自動縮小顯示的最小字體大小
text.minimumFontSize
=
20;
13、
adjustsFontSizeToFitWidth - 自適應文本窗口大小
text.adjustsFontSizeToFitWidth
=
YES;
- 設置爲YES時文本會自動縮小以適應文本窗口大小.默認是保持原來大小,而讓長文本滾動
14、
autocapitalizationType - 首字母是否大寫
text.autocapitalizationType
=
UITextAutocapitalizationTypeNone;
UITextAutocapitalizationTypeNone - 不自動大寫
UITextAutocapitalizationTypeWords - 單詞首字母大寫
UITextAutocapitalizationTypeSentences - 句子的首字母大寫
UITextAutocapitalizationTypeAllCharacters - 所有字母都大寫
15、
contentVerticalAlignment -
字的擺設方式
text.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
16、
Disabled - 若選中此項,用戶將不能更改文本框內容
(4)clearButtonMode - 設置一個清空按鈕
text.clearButtonMode
=
UITextFieldViewModeAlways;
- 通過設置 clearButtonMode 可以指定是否以及何時顯示清除按鈕
- 常見於密碼輸入錯誤,重新輸入時會清空輸入框
- UITextFieldViewModeNever - 從不顯示清空按鈕
- UITextFieldViewModeWhileEditing - 不爲空,且在編輯狀態時(即獲得焦點)顯示清空按鈕
- UITextFieldViewModeUnlessEditing - 除了編輯狀態時,都顯示清空按鈕
- UITextFieldViewModeAlways - 不爲空,一直都顯示清空按鈕
(5)設置鍵盤
1、keyboardType - 設置鍵盤樣式
text.keyboardType
=
UIKeyboardTypeNumberPad;
- UIKeyboardTypeDefault - 默認鍵盤,支持所有字符
- UIKeyboardTypeASCIICapable - 支持ASCII的默認鍵盤
- UIKeyboardTypeNumbersAndPunctuation - 標準電話鍵盤,支持+*#字符
- UIKeyboardTypeURL - URL鍵盤,支持.com按鈕
只支持URL字符
- UIKeyboardTypeNumberPad - 數字鍵盤
- UIKeyboardTypePhonePad - 電話鍵盤
- UIKeyboardTypeNamePhonePad - 電話鍵盤,也支持輸入人名
- UIKeyboardTypeEmailAddress - 用於輸入電子
郵件地址的鍵盤
- UIKeyboardTypeDecimalPad - 數字鍵盤
有數字和小數點
- UIKeyboardTypeTwitter - 優化的鍵盤,方便輸入@、#字符
- UIKeyboardTypeAlphabet - UIKeyboardTypeASCIICapable,
2、
returnKeyType - 返回鍵的樣式
text.returnKeyType
=UIReturnKeyDone;
-
UIReturnKeyDefault - 默認
灰色按鈕,標有Return
- UIReturnKeyGo
- 標有Go的藍色按鈕
- UIReturnKeyGoogle
- 標有Google的藍色按鈕,用於搜索
- UIReturnKeyJoin
- 標有Join的藍色按鈕
- UIReturnKeyNext
- 標有Next的藍色按鈕
- UIReturnKeyRoute
- 標有Route的藍色按鈕
- UIReturnKeySearch
- 標有Search的藍色按鈕
- UIReturnKeySend
- 標有Send的藍色按鈕
- UIReturnKeyYahoo
- 標有Yahoo的藍色按鈕
- UIReturnKeyDone
- 標有Done的藍色按鈕
- UIReturnKeyEmergencyCall
- 緊急呼叫按鈕
3、keyboardAppearance - 鍵盤外觀
text.keyboardAppearance=UIKeyboardAppearanceDefault;
- UIKeyboardAppearanceDefault - 默認外觀,淺灰色
- UIKeyboardAppearanceAlert - 深灰
石墨色
4、鍵盤是否隱藏
顯示
[text
becomeFirstResponder];
隱藏
[text
resignFirstResponder];
5、inputView - 設置一個UIView類型的自定義鍵盤
6、inputAccessoryView - 設置一個鍵盤的自定義工具條
7、鍵盤類型
- UIKeyboardTypeDefault 默認鍵盤
-
UIKeyboardTypeASCIICapable 顯示ASCII碼值得鍵盤
-
UIKeyboardTypeNumbersAndPunctuation 顯示數字和標點符號得鍵盤
-
UIKeyboardTypeURL 顯示帶有 . / .com URL常用得符號得鍵盤
-
UIKeyboardTypeNumberPad 顯示0到9得數字鍵盤 不支持自動大寫
-
UIKeyboardTypePhonePad 顯示帶有0到9和“*”,“#”得鍵盤 不支持自動大寫
-
UIKeyboardTypeNamePhonePad 顯示一個支持輸入一個聯繫人名字或者號碼得鍵盤 不支持自動大寫
-
UIKeyboardTypeEmailAddress 顯示一個支持輸入Email地址符號得鍵盤 “@”
-
UIKeyboardTypeDecimalPad 顯示0到9 和 “."得鍵盤
- UIKeyboardTypeAlphabet 顯示一個字母鍵盤
(6)設置代理
text.delegate
=
self;
- 設置代理,用於實現協議<UITextFieldDelegate>
對UITextField的操作:
@protocol
UITextViewDelegate <NSObject, UIScrollViewDelegate>
@optional
- (BOOL)textViewShouldBeginEditing:(UITextView
*)textView;
- (BOOL)textViewShouldEndEditing:(UITextView *)textView;
- (void)textViewDidBeginEditing:(UITextView *)textView;
- (void)textViewDidEndEditing:(UITextView *)textView;
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString
*)text;
- (void)textViewDidChange:(UITextView *)textView;
- (void)textViewDidChangeSelection:(UITextView
*)textView;
@end
三、UITextField 的方法
(1)、UITextField 的委託方法
- (BOOL)textFieldShouldBeginEditing:(UITextField
*)textField{
//返回一個BOOL值,指定是否循序文本字段開始編輯
return
YES;
}
- (void)textFieldDidBeginEditing:(UITextField
*)textField{
//開始編輯時觸發,文本字段將成爲first responder
}
- (BOOL)textFieldShouldEndEditing:(UITextField
*)textField{
//返回BOOL值,指定是否允許文本字段結束編輯,當編輯結束,文本字段會讓出first
responder
//要想在用戶結束編輯時阻止文本字段消失,可以返回NO
//這對一些文本字段必須始終保持活躍狀態的程序很有用,比如即時消息
return
NO;
}
- (BOOL)textField:(UITextField*)textField
shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString
*)string{
//當用戶使用自動更正功能,把輸入的文字修改爲推薦的文字時,就會調用這個方法。
//這對於想要加入撤銷選項的應用程序特別有用
//可以跟蹤字段內所做的最後一次修改,也可以對所有編輯做日誌記錄,用作審計用途。
//要防止文字被改變可以返回NO
//這個方法的參數中有一個NSRange對象,指明瞭被改變文字的位置,建議修改的文本也在其中
return
YES;
}
- (BOOL)textFieldShouldReturn:(UITextField
*)textField
{
[textField
resignFirstResponder];
//返回一個BOOL值,指明是否允許在按下回車鍵時結束編輯
//主要是[receiver resignFirstResponder]在哪調用就能把receiver對應的鍵盤往下收
return
YES;
}
- (BOOL)textFieldShouldClear:(UITextField
*)textField
{
//返回一個BOOL值指明是否允許根據用戶請求清除內容
//可以設置在特定條件下才允許清除內容
return
YES;
}
四、UITextField 的支持事件
(1)UITextField類特有的事件
【通知】
UITextField派生自UIControl,所以UIControl類中的通知系統在文本字段中也可以使用。除了UIControl類的標準事件,你還可以使用下列UITextField類特有的事件
-
UIKIT_EXTERN
NSString
*const UITextFieldTextDidBeginEditingNotification;
-
UIKIT_EXTERN
NSString
*const
UITextFieldTextDidEndEditingNotification;
-
UIKIT_EXTERN
NSString
*const
UITextFieldTextDidChangeNotification;
(2)當文本字段退出編輯模式時觸發。通知的object屬性存儲了最終文本。
因爲文本字段要使用鍵盤輸入文字,所以下面這些事件發生時,也會發送動作通知
-
UIKeyboardWillShowNotification
//鍵盤顯示之前發送
-
UIKeyboardDidShowNotification
//鍵盤顯示之後發送
-
UIKeyboardWillHideNotification
//鍵盤隱藏之前發送
-
UIKeyboardDidHideNotification
//鍵盤隱藏之後發送