標籤:
(一)常規操作
1.定義一個UITextField,名為textField:
UITextField *textField = = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 200, 40)];
2.設定背景顏色
textField.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:0 alpha:0.5];
3.??????設定邊框類型
textField.borderStyle = UITextBorderStyleRoundedRect;
4.設定背景圖片
textField???.background = [UIImage imageNamed:@""];
5.設定委派物件
textField.delegate = self;
6.設定預設提示文字
textField.placeholder = @"請輸入文字";
7.彈出鍵盤(變成第一響應者)
[textField becomeFirstResponder];
8.文字內容 垂直置中
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
(二)委託設定
1.在聲明中引入委託名( ViewController.m中)
@interface ViewController ()<UITextFieldDelegate/*這裡引入UITextField 需要的委託*/>{ //在這裡聲明全域 私人變數 UITextField *textField;}
2.設定委派物件
textField.delegate = self;
3.實現委託方法
//以下是UITextFieldDelegate 的部分委託實現//*******************************************************************************#pragma mark -------------------#pragma mark UITextFieldDelegate- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ // return NO to disallow editing. return YES;}- (void)textFieldDidBeginEditing:(UITextField *)textField{ // became first responder // 在這裡監聽UITextField becomeFirstResponder事件}- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{ // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end return YES;}- (void)textFieldDidEndEditing:(UITextField *)textField{ // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called // 在這裡監聽UITextField resignFirstResponder事件}- (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ // return NO to not change text NSLog(@"inputText: %@", textField.text); return
IOS代碼布局(三) UITextField