1 前言 UITextField用來接受用戶輸入的文本,在開發中十分常見,今天我們來學習一下該控件。 2 代碼實例 ZYViewController.h: #import <UIKit/UIKit.h> @interface ZYViewController : UIViewController @property (nonatomic,strong)UITextField *myTextField; @end #import <UIKit/UIKit.h> @interface ZYViewController : UIViewController @property (nonatomic,strong)UITextField *myTextField; @end ZYViewController.m: @synthesize myTextField; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor whiteColor]; CGRect textFieldFrame = CGRectMake(0.0f, 0.0f, 200.0f, 31.0f); self.myTextField = [[UITextField alloc] initWithFrame:textFieldFrame];//繪制控件模型 self.myTextField.borderStyle = UITextBorderStyleRoundedRect;//設置如何顯示邊框 self.myTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;//設置縱向居中 self.myTextField.textAlignment = UITextAlignmentCenter;//設置水平居中 self.myTextField.text = @"Sir Archy Develop_Zhang";//設置輸入框內容 self.myTextField.center = self.view.center;//設置UITextField位置 [self.view addSubview:self.myTextField]; } @synthesize myTextField; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor whiteColor]; CGRect textFieldFrame = CGRectMake(0.0f, 0.0f, 200.0f, 31.0f); self.myTextField = [[UITextField alloc] initWithFrame:textFieldFrame];//繪制控件模型 self.myTextField.borderStyle = UITextBorderStyleRoundedRect;//設置如何顯示邊框 self.myTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;//設置縱向居中 self.myTextField.textAlignment = UITextAlignmentCenter;//設置水平居中 self.myTextField.text = @"Sir Archy Develop_Zhang";//設置輸入框內容 self.myTextField.center = self.view.center;//設置UITextField位置 [self.view addSubview:self.myTextField]; } ZYUITextFieldViewController.h: #import <UIKit/UIKit.h> @interface ZYUITextFieldViewController : UIViewController<UITextFieldDelegate> @property(nonatomic,strong) UITextField *myTextField; @property(nonatomic,strong) UILabel *labelCounter; @end #import <UIKit/UIKit.h> @interface ZYUITextFieldViewController : UIViewController<UITextFieldDelegate> @property(nonatomic,strong) UITextField *myTextField; @property(nonatomic,strong) UILabel *labelCounter; @end ZYUITextFieldViewController.m: @synthesize myTextField; @synthesize labelCounter; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor whiteColor]; CGRect textFieldFrame = CGRectMake(38.0f, 30.0f, 220.0f, 31.0f); self.myTextField = [[UITextField alloc] initWithFrame:textFieldFrame]; self.myTextField.delegate = self; self.myTextField.borderStyle = UITextBorderStyleRoundedRect; self.myTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; // self.myTextField.text = @"Sir Archy Developer_Zhang"; self.myTextField.placeholder = @"Enter text here......";//當UITextField裡面沒有字符的時候,默認顯示的內容 [self.view addSubview:myTextField]; CGRect labelCounterFrame = self.myTextField.frame; labelCounterFrame.origin.y += textFieldFrame.size.height+10; self.labelCounter = [[UILabel alloc] initWithFrame:labelCounterFrame]; [self.view addSubview:labelCounter]; [self calculateAndDisplayTextFieldLengthWithText:self.myTextField.text]; UILabel *currencyLabel = [[UILabel alloc] initWithFrame:CGRectZero];//CGRectZero相當於CGRectMake(0,0,0,0) currencyLabel.text = [[[NSNumberFormatter alloc] init] currencySymbol];//初始化Label內容返回接受者的本地貨幣符號 currencyLabel.font = self.myTextField.font; [currencyLabel sizeToFit];//調整和移動接收者的視圖,它只是包含它自己的視圖。 self.myTextField.leftView = currencyLabel; self.myTextField.leftViewMode = UITextFieldViewModeAlways;//設置左視圖一直顯示 /* typedef enum{ UITextFieldViewModeNever,//從不顯示 UITextFieldViewModeWhileEditing,//編輯時候顯示 UITextFieldViewModeUnlessEditing,//編輯結束時候顯示 UITextFieldViewModeAlways//一直顯示 }UITextFieldViewMode; */ } //計算UITextField裡面字符的長度 -(void)calculateAndDisplayTextFieldLengthWithText:(NSString *)paramText{ NSString *characterOrCharacters = @"Characters"; if ([paramText length]==1) { characterOrCharacters = @"Character"; } self.labelCounter.text = [NSString stringWithFormat:@"%lu %@",(unsigned long)[paramText length],characterOrCharacters]; } //當UITextField輸入文字後觸發的事件 -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ BOOL result = YES; if ([textField isEqual:self.myTextField]) { NSString *wholeText = [textField.text stringByReplacingCharactersInRange:range withString:string];//追加後輸入的字符串 [self calculateAndDisplayTextFieldLengthWithText:wholeText];//重新計算字符長度 } return result; } //當單擊返回按鈕觸發的事件 -(BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder];//關閉鍵盤 return YES; } @synthesize myTextField; @synthesize labelCounter; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor whiteColor]; CGRect textFieldFrame = CGRectMake(38.0f, 30.0f, 220.0f, 31.0f); self.myTextField = [[UITextField alloc] initWithFrame:textFieldFrame]; self.myTextField.delegate = self; self.myTextField.borderStyle = UITextBorderStyleRoundedRect; self.myTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; // self.myTextField.text = @"Sir Archy Developer_Zhang"; self.myTextField.placeholder = @"Enter text here......";//當UITextField裡面沒有字符的時候,默認顯示的內容 [self.view addSubview:myTextField]; CGRect labelCounterFrame = self.myTextField.frame; labelCounterFrame.origin.y += textFieldFrame.size.height+10; self.labelCounter = [[UILabel alloc] initWithFrame:labelCounterFrame]; [self.view addSubview:labelCounter]; [self calculateAndDisplayTextFieldLengthWithText:self.myTextField.text]; UILabel *currencyLabel = [[UILabel alloc] initWithFrame:CGRectZero];//CGRectZero相當於CGRectMake(0,0,0,0) currencyLabel.text = [[[NSNumberFormatter alloc] init] currencySymbol];//初始化Label內容返回接受者的本地貨幣符號 currencyLabel.font = self.myTextField.font; [currencyLabel sizeToFit];//調整和移動接收者的視圖,它只是包含它自己的視圖。 self.myTextField.leftView = currencyLabel; self.myTextField.leftViewMode = UITextFieldViewModeAlways;//設置左視圖一直顯示 /* typedef enum{ UITextFieldViewModeNever,//從不顯示 UITextFieldViewModeWhileEditing,//編輯時候顯示 UITextFieldViewModeUnlessEditing,//編輯結束時候顯示 UITextFieldViewModeAlways//一直顯示 }UITextFieldViewMode; */ } //計算UITextField裡面字符的長度 -(void)calculateAndDisplayTextFieldLengthWithText:(NSString *)paramText{ NSString *characterOrCharacters = @"Characters"; if ([paramText length]==1) { characterOrCharacters = @"Character"; } self.labelCounter.text = [NSString stringWithFormat:@"%lu %@",(unsigned long)[paramText length],characterOrCharacters]; } //當UITextField輸入文字後觸發的事件 -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ BOOL result = YES; if ([textField isEqual:self.myTextField]) { NSString *wholeText = [textField.text stringByReplacingCharactersInRange:range withString:string];//追加後輸入的字符串 [self calculateAndDisplayTextFieldLengthWithText:wholeText];//重新計算字符長度 } return result; } //當單擊返回按鈕觸發的事件 -(BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder];//關閉鍵盤 return YES; }
運行結果: