#import "ViewController.h"
@interface ViewController () <UITextFieldDelegate>
@property (nonatomic, strong) UITextField *textField;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSDictionary *attributeDict = @{NSFontAttributeName:[UIFont systemFontOfSize:14], NSForegroundColorAttributeName:[UIColor redColor]};
NSAttributedString *attributeString = [[NSAttributedString alloc] initWithString:@"默認富文本文字" attributes:attributeDict];
// 初始化文本框 並 設置位置及大小
_textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, 200, 40)];
// 添加文本框
[self.view addSubview:_textField];
// 設置代理
_textField.delegate = self;
// 設置默認文字
_textField.text = @"默認文字";
// 設置文字字體和大小
_textField.font = [UIFont fontWithName:@"Arial" size:18];
// 設置文字顏色
_textField.textColor = [UIColor blackColor];
// 設置水印提示文字 (當輸入框沒有內容時顯示的灰色文字)
_textField.placeholder = @"請輸入內容";
// 設置默認富文本文字 和 水印提示富文本文字 會覆蓋以前字體大小及顏色
//_textField.attributedText = attributeString;
//_textField.attributedPlaceholder = attributeString;
// 設置文本框中文字屬性
//_textField.defaultTextAttributes = attributeDict;
// 設置內容對齊方式
_textField.textAlignment = NSTextAlignmentLeft;
/*
NSTextAlignmentLeft 左對齊 (默認)
NSTextAlignmentCenter 居中對齊
NSTextAlignmentRight 右對齊
NSTextAlignmentJustified Fully-justified. The last line in a paragraph is natural-aligned.
NSTextAlignmentNatural Indicates the default alignment for script
*/
// 設置文本框再次編輯就清空內容 (默認NO)
_textField.clearsOnBeginEditing = YES;
// 設置密文輸入 每輸入一個字符就變成點 (默認NO)
//_textField.secureTextEntry = YES;
// 設置輸入內容超出輸入框時 字體自動縮小以適應文本窗口大小 (默認NO 字體保持原來大小 文本滾動)
_textField.adjustsFontSizeToFitWidth = YES;
// 設置文本框可以顯示的最小字體 (最小是14)
_textField.minimumFontSize = 14;
// 設置鍵盤右下角的返回按鈕是disabled(灰色不可點擊) 當文本框中沒有輸入任何字符時 (默認NO)
_textField.enablesReturnKeyAutomatically = YES;
// 首字母是否大寫
_textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
/*
UITextAutocapitalizationTypeNone 不自動大寫 (默認)
UITextAutocapitalizationTypeWords 單詞首字母大寫
UITextAutocapitalizationTypeSentences 句子的首字母大寫
UITextAutocapitalizationTypeAllCharacters 所有字母都大寫
*/
// 設置清除按鈕出現方式 文本框右邊的小X
_textField.clearButtonMode = UITextFieldViewModeWhileEditing;
// 設置文本框左側視圖及出現方式
_textField.leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"left.png"]];
_textField.leftViewMode = UITextFieldViewModeAlways;
// 設置文本框右側視圖及出現方式 與清除按鈕衝突 需設置不同出現模式
_textField.rightView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];
_textField.rightViewMode = UITextFieldViewModeUnlessEditing;
/*
UITextFieldViewModeNever 從不出現 (默認)
UITextFieldViewModeWhileEditing 編輯時出現
UITextFieldViewModeUnlessEditing 除了編輯外都出現
UITextFieldViewModeAlways 總是可見
*/
// 設置邊框樣式,只有設置了纔會顯示邊框樣式
_textField.borderStyle = UITextBorderStyleNone;
/*
UITextBorderStyleNone // 無邊框 (默認)
UITextBorderStyleLine // 黑色直角邊框
UITextBorderStyleBezel // 灰色直角邊框 (帶陰影)
UITextBorderStyleRoundedRect // 淺灰色圓角邊框
*/
// 設置輸入框背景顏色
_textField.backgroundColor = [UIColor brownColor];
// 設置自定義背景圖片 (圖片被拉伸) (當UITextField的樣式爲UITextBorderStyleNone的時 才能修改此屬性) 設置後邊框會被忽略掉 顏色無效
_textField.background = [UIImage imageNamed:@"background2.png"];
// 設置當enable爲no時 文本框背景圖片
//_textField.enabled = NO;
//_textField.disabledBackground = [UIImage imageNamed:@"background1.png"];
// 設置是否自動糾錯
_textField.autocorrectionType = UITextAutocorrectionTypeDefault;
/*
UITextAutocorrectionTypeDefault 自動糾錯 默認
UITextAutocorrectionTypeNo 不自動糾錯
UITextAutocorrectionTypeYes 自動糾錯
*/
// 設置彈出鍵盤樣式
_textField.keyboardType = UIKeyboardTypeDefault;
/*
UIKeyboardTypeDefault 支持所有字符 (默認鍵盤)
UIKeyboardTypeASCIICapable 支持ASCII的默認鍵盤
UIKeyboardTypeNumbersAndPunctuation 標準電話鍵盤,支持+*#字符
UIKeyboardTypeURL URL鍵盤,支持.com按鈕 只支持URL字符
UIKeyboardTypeNumberPad 數字鍵盤
UIKeyboardTypePhonePad 電話鍵盤
UIKeyboardTypeNamePhonePad 電話鍵盤,也支持輸入人名
UIKeyboardTypeEmailAddress 用於輸入電子 郵件地址的鍵盤
UIKeyboardTypeDecimalPad 數字鍵盤 有數字和小數點
UIKeyboardTypeTwitter 優化的鍵盤,方便輸入@、#字符
*/
// 設置鍵盤return鍵樣式
_textField.returnKeyType = UIReturnKeySend;
/*
UIReturnKeyDefault 標有Return的灰色按鈕 (默認)
UIReturnKeyGo 標有Go的藍色按鈕
UIReturnKeyGoogle 標有Google的藍色按鈕,用語搜索
UIReturnKeyJoin 標有Join的藍色按鈕
UIReturnKeyNext 標有Next的藍色按鈕
UIReturnKeyRoute 標有Route的藍色按鈕
UIReturnKeySearch 標有Search的藍色按鈕
UIReturnKeySend 標有Send的藍色按鈕
UIReturnKeyYahoo 標有Yahoo的藍色按鈕
UIReturnKeyYahoo 標有Yahoo的藍色按鈕
UIReturnKeyEmergencyCall 緊急呼叫按鈕
*/
// 設置鍵盤外觀
_textField.keyboardAppearance = UIKeyboardAppearanceDefault;
/*
UIKeyboardAppearanceDefault 淺灰色 (默認)
UIKeyboardAppearanceAlert 深灰 石墨色
*/
// 設置輸入框成爲第一響應時彈出的視圖和輔助視圖 (自定義鍵盤)
//_textField.inputView = [UIView new];
//_textField.inputAccessoryView = [UIView new];
// 文本框是否正在編輯 readonly
BOOL isEditing = _textField.isEditing;
// 是否允許更改文本屬性字典 默認NO
_textField.allowsEditingTextAttributes = YES;
// 當選擇改變時自動重置文本屬性
_textField.typingAttributes = attributeDict;
// 設置內容垂直、水平對齊方式 UITextField繼承自UIControl,此類中有一個屬性contentVerticalAlignment
_textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
_textField.contentHorizontalAlignment = UIControlContentVerticalAlignmentCenter;
// 設置再次編輯時 原內容不消失 輸入內容替代原內容 再次點擊文本框可再原內容後接着輸入 與clearsOnBeginEditing設置消失 此屬性默認NO
_textField.clearsOnInsertion = YES;
// 結束編輯
//[_textField endEditing:YES];
// 註銷第一響應 (收起鍵盤)
[_textField resignFirstResponder];
// 成爲第一響應
[_textField becomeFirstResponder];
}
#pragma mark - UITextFieldDelegate
// 設置是否可以進入編輯狀態 設NO將無法修改 不出現鍵盤 YES可以修改 默認值
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
return YES;
}
// 當文本框成爲 FirstResponder 時執行
- (void)textFieldDidBeginEditing:(UITextField *)textField {
NSLog(@"BeginEditing");
}
// 設置是否可以結束編輯狀態 設YES文本框將失去FirstResponder
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
// 要想在用戶結束編輯時阻止文本字段消失,可以返回NO 這對一些文本字段必須始終保持活躍狀態的程序很有用,比如即時消息
return YES;
}
// 當文本框失去 FirstResponder 時執行
- (void)textFieldDidEndEditing:(UITextField *)textField {
NSLog(@"EndEditing");
}
// 文本框內容是否能被修改 每改變一次就會執行
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSLog(@"ChangeCharacters");
// 可以跟蹤字段內所做的最後一次修改,也可以對所有編輯做日誌記錄,用作審計用途。
// NSRange 被改變文字的位置
return YES;
}
// 設置是否允許 根據用戶請求清除內容
- (BOOL)textFieldShouldClear:(UITextField *)textField {
NSLog(@"Clear");
// 可以設置在特定條件下才允許清除內容
return YES;
}
// 當點擊鍵盤的返回鍵(右下角)時執行
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(@"Return");
// 如果允許要調用resignFirstResponder方法 這會導致結束編輯 如果textFieldShouldEndEditing返回NO 鍵盤不能收起
if (_textField == textField) {
[_textField resignFirstResponder];
}
return YES;
}
#pragma mark - 通知
// UITextField派生自UIControl,所以UIControl類中的通知系統在文本字段中也可以使用。除了UIControl類的標準事件,你還可以使用下列UITextField類特有的事件
/*
UITextFieldTextDidBeginEditingNotification
UITextFieldTextDidChangeNotification
UITextFieldTextDidEndEditingNotification
當文本字段退出編輯模式時觸發。通知的object屬性存儲了最終文本。
因爲文本字段要使用鍵盤輸入文字,所以下面這些事件發生時,也會發送動作通知
UIKeyboardWillShowNotification // 鍵盤顯示之前發送
UIKeyboardDidShowNotification // 鍵盤顯示之後發送
UIKeyboardWillHideNotification // 鍵盤隱藏之前發送
UIKeyboardDidHideNotification // 鍵盤隱藏之後發送
*/
#pragma mark - 重寫繪製
// 除了UITextField對象的風格選項,你還可以定製化UITextField對象,爲他添加許多不同的重寫方法,來改變文本字段的顯示行爲。這些方法都會返回一個CGRect結構,制定了文本字段每個部件的邊界範圍。以下方法都可以重寫。
/*
- (CGRect)borderRectForBounds:(CGRect)bounds; // 重寫來重置邊緣區域
- (CGRect)textRectForBounds:(CGRect)bounds; // 重寫來重置文字區域
- (CGRect)placeholderRectForBounds:(CGRect)bounds; // 重寫來重置佔位符區域
- (CGRect)editingRectForBounds:(CGRect)bounds; // 重寫來重置編輯區域
- (CGRect)clearButtonRectForBounds:(CGRect)bounds; // 重寫來重置clearButton位置,改變size可能導致button的圖片失真
- (CGRect)leftViewRectForBounds:(CGRect)bounds; // 重寫來重置leftView位置
- (CGRect)rightViewRectForBounds:(CGRect)bounds; // 重寫來重置rightView位置
- (void)drawTextInRect:(CGRect)rect; // 改變繪文字屬性.重寫時調用super可以按默認圖形屬性繪製,若自己完全重寫繪製函數,就不用調用super了.
- (void)drawPlaceholderInRect:(CGRect)rect; // 重寫改變繪製佔位符屬性.重寫時調用super可以按默認圖形屬性繪製,若自己完全重寫繪製函數,就不用調用super了.
*/
@end
UITextField詳解
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.