將一個圖片作為視圖的背景代碼如下:
- (void)addImageView
{
//找到圖片
UIImage *pImage=[UIImage imageNamed:@"sea.png"];
//根據內容圖片創建ImageView
self.pImageView = [[UIImageView alloc] initWithImage:pImage];
//將ImageView放到父視圖中
[self.view insertSubview:self.pImageView atIndex:0];
}
對Label的操作代碼如下
- (void)addLabel
{
//創建一個Label
UILabel *pLabel=[[UILabel alloc] initWithFrame:CGRectMake(10, 10, 120, 50)];
//內容
pLabel.text=@"HelloWorld\nSecondLine";
//設置字體和大小
pLabel.textAlignment=NSTextAlignmentCenter;
//字體顏色
pLabel.textColor=[UIColor redColor];
//顯示行數
pLabel.numberOfLines= 2;
//陰影顏色
pLabel.shadowColor=[UIColor blackColor];
//陰影尺寸
pLabel.shadowOffset = CGSizeMake(2.0, 1.0);
//設置label的背景為透明色
pLabel.backgroundColor=[UIColor clearColor];
[self.view addSubview:pLabel];
[pLabel release];
}
文本框的操作代碼如下:
- (void)addTextField
{
//創建TextField
UITextField *pTextField=[[UITextField alloc] initWithFrame:CGRectMake(10, 116, 200, 31)];
//設置邊框樣式
pTextField.borderStyle = UITextBorderStyleRoundedRect;
//設置字體
pTextField.font = [UIFont systemFontOfSize:18.0];
//根據寬度改變字體
pTextField.adjustsFontSizeToFitWidth = YES;
//最小字體
pTextField.minimumFontSize = 2.0;
//清除按鈕的樣式
pTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
//彈出鍵盤的樣式
pTextField.keyboardType = UIKeyboardTypeDefault;
//設置使用自動更正功能
pTextField.autocorrectionType= UITextAutocorrectionTypeNo;
//設置鍵盤自動大小寫的屬性
pTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
//設置返回按鈕的類型
pTextField.returnKeyType = UIReturnKeyDone;
//設置是否支持密碼文本顯示
pTextField.secureTextEntry = YES;
//設置委托
pTextField.delegate = self;
[self.view addSubview:pTextField];
[pTextField release];
}
按鈕的操作代碼如下:
//控制控件
- (void)addButton
{
//創建一個按鈕
UIButton *pBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//設置區域
[pBtn setFrame:CGRectMake(10, 70, 100, 40)];
[pBtn setTitle:@"Normal" forState:UIControlStateNormal];
[pBtn setTitle:@"HighLight" forState:UIControlStateHighlighted];
//允許顯示高亮
pBtn.showsTouchWhenHighlighted = YES;
[pBtn addTarget:self action:@selector(buttonDown:) forControlEvents:UIControlEventTouchDown];
[pBtn addTarget:self action:@selector(buttonRelease:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:pBtn];
}