標籤: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的鍵盤