文字欄位的重要屬性如下:
不需要使用者輸入預留位置文字時顯示
普通文字
自動校正型別
鍵盤型別
返回鍵的型別
清除按鈕模式
對齊方式
委託
可以在 xib 更改文字欄位屬性在屬性檢查在工具程式區(右側視窗)。
我們可以設定在Interface Builder代表右擊UIElement的將它連線到檔案的所有者,如下圖所示。
使用委託的步驟
1. 如上圖所示,設定委託
2. 委託類響應
3. 實現 TextField 委託,最重要的文字欄位委託如下
- (void)textFieldDidBeginEditing:(UITextField *)textField
- (void)textFieldDidEndEditing:(UITextField *)textField
4. 正如其名稱所義,一旦我們開始編輯的文字欄位和結束分別編輯上述兩個委託被呼叫。
5. 對於其他代表,請參閱UITextDelegate協定參考。
範例程式碼和步驟
1. 我們將使用範例應用程式建立UI元素
2. ViewController 類會採用 UITextFieldDelegate,更新如下我們的ViewController.h檔案。
#import <UIKit/UIKit.h> // You can notice the adddition of UITextFieldDelegate below @interface ViewController : UIViewController<UITextFieldDelegate> @end
3. 然後,我們新增了一個的方法addTextField 在 ViewController.m 檔案。
4. 在 viewDidLoad方法中呼叫此方法。
5. 更新 ViewController.m 和 viewDidLoad 如下
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //The custom method to create our textfield is called [self addTextField]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)addTextField{ // This allocates a label UILabel *prefixLabel = [[UILabel alloc]initWithFrame:CGRectZero]; //This sets the label text prefixLabel.text =@"## "; // This sets the font for the label [prefixLabel setFont:[UIFont boldSystemFontOfSize:14]]; // This fits the frame to size of the text [prefixLabel sizeToFit]; // This allocates the textfield and sets its frame UITextField *textField = [[UITextField alloc] initWithFrame: CGRectMake(20, 50, 280, 30)]; // This sets the border style of the text field textField.borderStyle = UITextBorderStyleRoundedRect; textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; [textField setFont:[UIFont boldSystemFontOfSize:12]]; //Placeholder text is displayed when no text is typed textField.placeholder = @"Simple Text field"; //Prefix label is set as left view and the text starts after that textField.leftView = prefixLabel; //It set when the left prefixLabel to be displayed textField.leftViewMode = UITextFieldViewModeAlways; // Adds the textField to the view. [self.view addSubview:textField]; // sets the delegate to the current class textField.delegate = self; } // pragma mark is used for easy access of code in Xcode #pragma mark - TextField Delegates // This method is called once we click inside the textField -(void)textFieldDidBeginEditing:(UITextField *)textField{ NSLog(@"Text field did begin editing"); } // This method is called once we complete editing -(void)textFieldDidEndEditing:(UITextField *)textField{ NSLog(@"Text field ended editing"); } // This method enables or disables the processing of return key -(BOOL) textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder]; return YES; } - (void)viewDidUnload { label = nil; [super viewDidUnload]; } @end
6. 現在,當我們執行程式時,我們會得到下面的輸出。
7. 委託方法被呼叫基於使用者操作。檢視控制台輸出,知道什麼時候被呼叫委託。