UITextField常用屬性與回收UITextfield的鍵盤

來源:互聯網
上載者:User

標籤:style   color   io   使用   ar   檔案   sp   on   c   

UITextField 是UIControl的子類,UIControl又是UIView的子類,所以也是一個視圖,只不過比UIView多了兩個功能,1.文字顯示,2.文本編輯   

使用過程分四步:   

1.建立對象   

2.配置屬性   

3添加到父視圖   

4.釋放所有權      

1.建立對象   

UITextField *text = [[UITextFieldalloc]initWithFrame:CGRectMake(50,50,180,50)];    text.backgroundColor = [UIColoryellowColor];    [_viewaddSubview:text];    [textrelease];

2.設定text的邊框樣式(圓角)   

text.borderStyle =UITextBorderStyleRoundedRect;       

3.設定text預設顯示文字(但是不作為常值內容的一部分)   

text.placeholder =@"請輸入使用者名稱";       

4.設定text文字   

text.text =@"什麼破爛”;


5.設定文本顏色   

text.textColor = [UIColorblackColor];

6.設定文本的對齊   

text.textAlignment = NSTextAlignmentCenter;

7.設定文字字型   

text.font = [UIFont systemFontOfSize:18];     

8.設定輸入框是否可編輯   

text.enabled =YES;

9.設定當開始編輯時,是否清除框中內容   

text.clearsOnBeginEditing =YES;       

10.設定密碼格式(輸入框中內容是否以點的形式顯示)   

text.secureTextEntry = YES;       

11.設定彈出鍵盤的樣式(數字鍵台)   

text.keyboardType = UIKeyboardTypeNumberPad;       

12.鍵盤右下角顯示樣式   

text.returnKeyType =UIReturnKeyGo;


13.設定tag值
text.tag = 120;


14.鍵盤迴收


1.點擊右下角或者斷行符號回收鍵盤
1.設定代理
text.delegate =self;

2.服從協議
在相應.h檔案添加協議,如
@interface MAYAppDelegate :UIResponder <UIApplicationDelegate,UITextFieldDelegate>
服從,<UITextFieldDelegate>協議

3.實現協議中的方法
 - (BOOL)textFieldShouldReturn:(UITextField *)textField

{  

   回收鍵盤,取消第一響應者   

  [textField resignFirstResponder];   returnYES;

}

2.點擊空白處回收鍵盤
1.設定代理
text.delegate = self;

2.服從協議
在相應的.h檔案中添加協議,如
@interface MAYAppDelegate : UIResponder <UIApplicationDelegate,UITextFieldDelegate>
服從,<UITextFieldDelegate>協議

3.實現協議中的方法

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

  [text resignFirstResponder];

}  

UITextField常用屬性與回收UITextfield的鍵盤

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.