註冊或登錄頁面
如下圖
1,這裡為瞭展現UITextField的文本框關聯鍵盤的設置,這裡把“密碼”和“確定密碼”的關聯鍵盤都設置為數字鍵盤,實際應用中密碼一般都允許為數字或字母。
2,實現瞭鍵盤收回操作。
3,這裡沒有寫對“用戶名”進行特殊字符過濾的代碼。
實現代碼:
vcD4KPHByZSBjbGFzcz0=”brush:java;”>#import “ViewController.h”
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//三個UILabel
UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 60, 80, 37)];
nameLabel.font = [UIFont systemFontOfSize:15];
nameLabel.text = @”用 戶 名:”;
nameLabel.backgroundColor = [UIColor clearColor];
nameLabel.textAlignment = NSTextAlignmentLeft;
nameLabel.numberOfLines = 2; //用於設置UILabel中文本的行數
[self.view addSubview:nameLabel];
[nameLabel release];
UILabel *newPasswordLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 60+40, 80, 37)];
newPasswordLabel.font = [UIFont systemFontOfSize:15];
newPasswordLabel.text = @”密 碼:”;
newPasswordLabel.backgroundColor = [UIColor clearColor];
newPasswordLabel.textAlignment = NSTextAlignmentLeft;
[self.view addSubview:newPasswordLabel];
[newPasswordLabel release];
UILabel *oncePasswordLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 60+40*2, 80, 37)];
oncePasswordLabel.font = [UIFont systemFontOfSize:15];
oncePasswordLabel.text = @”確認密碼:”;
oncePasswordLabel.backgroundColor = [UIColor clearColor];
oncePasswordLabel.textAlignment = NSTextAlignmentLeft;
[self.view addSubview:oncePasswordLabel];
[oncePasswordLabel release];
//三個輸入框
UITextField *nameTextField = [[UITextField alloc]initWithFrame:CGRectMake(90, 60, 210, 30)];
nameTextField.placeholder = @”請輸入用戶名”;
nameTextField.tag = 1;
[nameTextField setSecureTextEntry:NO];
nameTextField.font = [UIFont systemFontOfSize:14];
nameTextField.delegate = self;
nameTextField.backgroundColor = [UIColor clearColor];
nameTextField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:nameTextField];
[nameTextField release];
UITextField *passwordTextField = [[UITextField alloc]initWithFrame:CGRectMake(90, 60+40, 210, 30)];
passwordTextField.placeholder = @”至少6位數字”;
passwordTextField.tag = 2;
[passwordTextField setSecureTextEntry:YES];
passwordTextField.font = [UIFont systemFontOfSize:14];
passwordTextField.delegate = self;
passwordTextField.backgroundColor = [UIColor clearColor];
passwordTextField.borderStyle = UITextBorderStyleRoundedRect;
passwordTextField.keyboardType = UIKeyboardTypeNumberPad;
[self.view addSubview:passwordTextField];
[passwordTextField release];
UITextField *onceNewPasswordTextField = [[UITextField alloc]initWithFrame:CGRectMake(90, 60+40*2, 210, 30)];
onceNewPasswordTextField.placeholder = @”請再次輸入密碼”;
onceNewPasswordTextField.tag = 3;
onceNewPasswordTextField.font = [UIFont systemFontOfSize:14];
[onceNewPasswordTextField setSecureTextEntry:YES];
onceNewPasswordTextField.delegate = self;
onceNewPasswordTextField.backgroundColor = [UIColor clearColor];
onceNewPasswordTextField.borderStyle = UITextBorderStyleRoundedRect;
onceNewPasswordTextField.keyboardType = UIKeyboardTypeNumberPad;
[self.view addSubview:onceNewPasswordTextField];
[onceNewPasswordTextField release];
UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
confirmButton.frame = CGRectMake(110, 60+40*3+20, 100, 37);
[confirmButton setTitle:@”確定” forState:UIControlStateNormal]; //正常狀況下button顯示的標題
[confirmButton setTitle:@”確定” forState:UIControlStateHighlighted]; //高亮顯示時button的標題
confirmButton.backgroundColor = [UIColor redColor];
[confirmButton addTarget:self action:@selector(confirm:) forControlEvents:UIControlEventTouchUpInside];//button被按下又抬起後發生的事件
//@selector可以理解為”選擇子”,selector是一個指針變量,類似於sender。 這裡是將method的方法指定給新建的這個confirmButton
[self.view addSubview:confirmButton];
}
//收回鍵盤
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for (int i = 0; i<4; i++) {
UITextField *textField = (UITextField*)[self.view viewWithTag:1+i];
[textField resignFirstResponder];
}
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
UILabel
UILabel繼承瞭UIView,它可以設置UIView所支持的屬性。
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 20.0, 200.0, 50.0)]; //設置Label的位置和大小 //設置顯示文字 label1.text = @"用戶名"; //設置字體:粗體,正常的是 SystemFontOfSize label1.font = [UIFont boldSystemFontOfSize:20]; //設置文字顏色 label1.textColor = [UIColor orangeColor]; //設置文字位置 label1.textAlignment = UITextAlignmentRight; label2.textAlignment = UITextAlignmentCenter; //設置字體大小適應label寬度 label4.adjustsFontSizeToFitWidth = YES; //設置label的行數 label5.numberOfLines = 2; UIlabel.backgroudColor=[UIColor clearColor]; //可以去掉背景色 //設置高亮 label6.highlighted = YES; label6.highlightedTextColor = [UIColor orangeColor]; //設置陰影 label7.shadowColor = [UIColor redColor]; label7.shadowOffset = CGSizeMake(1.0,1.0); //設置是否能與用戶進行交互 label7.userInteractionEnabled = YES; //設置label中的文字是否可變,默認值是YES label3.enabled = NO; //設置文字過長時的顯示格式 label3.lineBreakMode = UILineBreakModeMiddleTruncation;//截去中間 // typedef enum { // UILineBreakModeWordWrap = 0, // UILineBreakModeCharacterWrap, // UILineBreakModeClip,//截去多餘部分 // UILineBreakModeHeadTruncation,//截去頭部 // UILineBreakModeTailTruncation,//截去尾部 // UILineBreakModeMiddleTruncation,//截去中間 // } UILineBreakMode; //如果adjustsFontSizeToFitWidth屬性設置為YES,這個屬性就來控制文本基線的行為 label4.baselineAdjustment = UIBaselineAdjustmentNone; // typedef enum { // UIBaselineAdjustmentAlignBaselines, // UIBaselineAdjustmentAlignCenters, // UIBaselineAdjustmentNone, // } UIBaselineAdjustment;
有時需要設置UILabel中文本的行數,其屬性值默認為1,用於設置該UILabel隻能顯示一行文本。
oldPasswordLabel.numberOfLines = 2;
UITextField
//初始化textfield並設置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, 30)]; //當輸入框沒有內容時,水印提示 ,提示內容為“用戶名” //顯示灰色字體,作為提示信息 text.placeholder = @"用戶名"; //設置邊框樣式,隻有設置瞭才會顯示邊框樣式 text.borderStyle = UITextBorderStyleRoundedRect; typedef enum { UITextBorderStyleNone, UITextBorderStyleLine, UITextBorderStyleBezel, UITextBorderStyleRoundedRect } UITextBorderStyle; //設置鍵盤的樣式 text.keyboardType = UIKeyboardTypeNumberPad; typedef enum { UIKeyboardTypeDefault, //默認鍵盤,支持所有字符 UIKeyboardTypeASCIICapable, //支持ASCII的默認鍵盤 UIKeyboardTypeNumbersAndPunctuation, //標準電話鍵盤,支持+*#字符 UIKeyboardTypeURL, //URL鍵盤,支持.com按鈕 隻支持URL字符 UIKeyboardTypeNumberPad, //數字鍵盤 UIKeyboardTypePhonePad, //電話鍵盤 UIKeyboardTypeNamePhonePad, //電話鍵盤,也支持輸入人名 UIKeyboardTypeEmailAddress, //用於輸入電子 郵件地址的鍵盤 UIKeyboardTypeDecimalPad, //數字鍵盤 有數字和小數點 UIKeyboardTypeTwitter, //優化的鍵盤,方便輸入@、#字符 UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, } UIKeyboardType; //每輸入一個字符就變成點 用語密碼輸入 text.secureTextEntry = YES; //設置輸入框的背景顏色,此時設置為白色 如果使用瞭自定義的背景圖片邊框會被忽略掉 text.backgroundColor = [UIColor whiteColor]; //設置背景圖片 text.background = [UIImage imageNamed:@"dd.png"]; //設置背景 text.disabledBackground = [UIImage imageNamed:@"cc.png"]; //設置輸入框內容的字體樣式和大小 text.font = [UIFont fontWithName:@"Arial" size:20.0f]; //設置字體顏色 text.textColor = [UIColor redColor]; //輸入框中是否有個叉號,在什麼時候顯示,用於一次性刪除輸入框中的內容 text.clearButtonMode = UITextFieldViewModeAlways; typedef enum { UITextFieldViewModeNever, //從不出現 UITextFieldViewModeWhileEditing, //編輯時出現 UITextFieldViewModeUnlessEditing, //除瞭編輯外都出現 UITextFieldViewModeAlways //一直出現 } UITextFieldViewMode; //輸入框中一開始就有的文字 text.text = @"一開始就在輸入框的文字"; //是否糾錯 text.autocorrectionType = UITextAutocorrectionTypeNo; typedef enum { UITextAutocorrectionTypeDefault, //默認 UITextAutocorrectionTypeNo, //不自動糾錯 UITextAutocorrectionTypeYes, //自動糾錯 } UITextAutocorrectionType; //再次編輯就清空 text.clearsOnBeginEditing = YES; //內容對齊方式 text.textAlignment = UITextAlignmentLeft; //內容的垂直對齊方式 UITextField繼承自UIControl,此類中有一個屬性contentVerticalAlignment text.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; //設置為YES時文本會自動縮小以適應文本窗口大小.默認是保持原來大小,而讓長文本滾動 textFied.adjustsFontSizeToFitWidth = YES; //設置自動縮小顯示的最小字體大小 text.minimumFontSize = 20; //首字母是否大寫 text.autocapitalizationType = UITextAutocapitalizationTypeNone; typedef enum { UITextAutocapitalizationTypeNone, 不自動大寫 UITextAutocapitalizationTypeWords, 單詞首字母大寫 UITextAutocapitalizationTypeSentences, 句子的首字母大寫 UITextAutocapitalizationTypeAllCharacters, 所有字母都大寫 } UITextAutocapitalizationType; //return鍵變成什麼鍵 text.returnKeyType =UIReturnKeyDone; typedef enum { UIReturnKeyDefault, //默認 灰色按鈕,標有Return UIReturnKeyGo, //標有Go的藍色按鈕 UIReturnKeyGoogle, //標有Google的藍色按鈕,用語搜索 UIReturnKeyJoin, //標有Join的藍色按鈕 UIReturnKeyNext, //標有Next的藍色按鈕 UIReturnKeyRoute, //標有Route的藍色按鈕 UIReturnKeySearch, //標有Search的藍色按鈕 UIReturnKeySend, //標有Send的藍色按鈕 UIReturnKeyYahoo, //標有Yahoo的藍色按鈕 UIReturnKeyYahoo, //標有Yahoo的藍色按鈕 UIReturnKeyEmergencyCall, //緊急呼叫按鈕 } UIReturnKeyType; //鍵盤外觀 textView.keyboardAppearance=UIKeyboardAppearanceDefault; typedef enum { UIKeyboardAppearanceDefault, //默認外觀,淺灰色 UIKeyboardAppearanceAlert, //深灰 石墨色 } UIReturnKeyType; //設置代理 用於實現協議 text.delegate = self; //把textfield加到視圖中 [self.window addSubview:text]; //最右側加圖片是以下代碼 左側類似 UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]]; text.rightView=image; text.rightViewMode = UITextFieldViewModeAlways; typedef enum { UITextFieldViewModeNever, UITextFieldViewModeWhileEditing, UITextFieldViewModeUnlessEditing, UITextFieldViewModeAlways } UITextFieldViewMode;
在處理密碼等隱私類的信息時,可能需要將輸入的信息隱藏一下。
//每輸入一個字符就變成點 ,用語密碼輸入 [passwordTextField setSecureTextEntry:YES];
也可以設置文本框關聯的鍵盤,如下:
//設置鍵盤的樣式 text.keyboardType = UIKeyboardTypeNumberPad; typedef enum { UIKeyboardTypeDefault, //默認鍵盤,支持所有字符 UIKeyboardTypeASCIICapable, //支持ASCII的默認鍵盤 UIKeyboardTypeNumbersAndPunctuation, //標準電話鍵盤,支持+*#字符 UIKeyboardTypeURL, //URL鍵盤,支持.com按鈕 隻支持URL字符 UIKeyboardTypeNumberPad, //數字鍵盤 UIKeyboardTypePhonePad, //電話鍵盤 UIKeyboardTypeNamePhonePad, //電話鍵盤,也支持輸入人名 UIKeyboardTypeEmailAddress, //用於輸入電子 郵件地址的鍵盤 UIKeyboardTypeDecimalPad, //數字鍵盤 有數字和小數點 UIKeyboardTypeTwitter, //優化的鍵盤,方便輸入@、#字符 UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, } UIKeyboardType;
有時需要限制輸入文本的長度,這類操作也非常普遍和重要。
//限制輸入文本的長度 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if ([textField.text length] > MAXLENGTH) { textField.text = [textField.text substringToIndex:MAXLENGTH-1]; return NO; } return YES; }
UIButton
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; // 能夠定義的button類型有以下6種, // typedef enum { // UIButtonTypeCustom = 0, 自定義風格 // UIButtonTypeRoundedRect, 圓角矩形 // UIButtonTypeDetailDisclosure, 藍色小箭頭按鈕,主要做詳細說明用 // UIButtonTypeInfoLight, 亮色感嘆號 // UIButtonTypeInfoDark, 暗色感嘆號 // UIButtonTypeContactAdd, 十字加號按鈕 // } UIButtonType; //給定button在view上的位置 button1.frame = CGRectMake(20, 20, 280, 20); //button背景色 button1.backgroundColor = [UIColor clearColor]; //設置button填充圖片 //[button1 setImage:[UIImage imageNamed:@"btng.png"] forState:UIControlStateNormal]; //設置button標題 [button1 setTitle:@"點擊" forState:UIControlStateNormal]; /* forState: 這個參數的作用是定義按鈕的文字或圖片在何種狀態下才會顯現*/ //以下是幾種狀態 // enum { // UIControlStateNormal = 0, 常規狀態顯現 // UIControlStateHighlighted = 1 << 0, 高亮狀態顯現 // UIControlStateDisabled = 1 << 1, 禁用的狀態才會顯現 // UIControlStateSelected = 1 << 2, 選中狀態 // UIControlStateApplication = 0x00FF0000, 當應用程序標志時 // UIControlStateReserved = 0xFF000000 為內部框架預留,可以不管他 // }; /* * 默認情況下,當按鈕高亮的情況下,圖像的顏色會被畫深一點,如果這下面的這個屬性設置為no, * 那麼可以去掉這個功能 */ button1.adjustsImageWhenHighlighted = NO; /*跟上面的情況一樣,默認情況下,當按鈕禁用的時候,圖像會被畫得深一點,設置NO可以取消設置*/ button1.adjustsImageWhenDisabled = NO; /* 下面的這個屬性設置為yes的狀態下,按鈕按下會發光*/ button1.showsTouchWhenHighlighted = YES; /* 給button添加事件,事件有很多種,我會單獨開一篇博文介紹它們,下面這個時間的意思是 按下按鈕,並且手指離開屏幕的時候觸發這個事件,跟web中的click事件一樣。 觸發瞭這個事件以後,執行butClick:這個方法,addTarget:self 的意思是說,這個方法在本類中 也可以傳入其他類的指針*/ [button1 addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside]; //顯示控件 [self.view addSubview:button1];
單獨說明一下:
UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; confirmButton.frame = CGRectMake(110, 60+40*3+20, 100, 37); [confirmButton setTitle:@"確定" forState:UIControlStateNormal]; //正常狀況下button顯示的標題 [confirmButton setTitle:@"確定" forState:UIControlStateHighlighted]; //高亮顯示時button的標題 confirmButton.backgroundColor = [UIColor redColor]; [confirmButton addTarget:self action:@selector(confirm:) forControlEvents:UIControlEventTouchUpInside];//button被按下又抬起後發生的事件 //@selector可以理解為"選擇子",selector是一個指針變量,類似於sender。 這裡是將method的方法指定給新建的這個confirmButton [self.view addSubview:confirmButton];
若要設置UIButton的背景圖片時:
UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom]; confirmButton.frame = CGRectMake(10, 60, 100, 40); UIImage *nextStepImage = [UIImage imageNamed:@"app.png"]; UIImage *nextStepDownImage = [UIImage imageNamed:@"app.png"]; nextStepImage = [nextStepImage resizableImageWithCapInsets:UIEdgeInsetsMake(8, 8, 8, 8)]; nextStepDownImage = [nextStepDownImage resizableImageWithCapInsets:UIEdgeInsetsMake(8, 8, 8, 8)]; [confirmButton setBackgroundImage:nextStepImage forState:UIControlStateNormal]; [confirmButton setBackgroundImage:nextStepDownImage forState:UIControlStateHighlighted]; [confirmButton setTitle:@"確定" forState:UIControlStateNormal]; [confirmButton addTarget:self action:@selector(confirm:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:confirmButton];