@interface ViewController ()web
@endapp
@implementation ViewController框架
(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.ide
//三個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];code
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];orm
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+403+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;
*********在處理密碼等隱私類的信息時,可能須要將輸入的信息隱藏一下。
//每輸入一個字符就變成點 ,用語密碼輸入
[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;
有時須要限制輸入文本的長度,這類操做也很是廣泛和重要。
//限制輸入文本的長度
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+403+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];