一. UIView
/* UIView 的創(chuàng)建 */
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 100, 30)];
/* 設(shè)置UIView背景顏色 */
view.backgroundColor = [UIColor cyanColor];
/* 使用取色器自定義RBG顏色的方法 */
view.backgroundColor = [UIColor colorWithRed:255 / 255 green:102 / 255 blue:255 / 255 alpha:1];//找到你想要的顏色RBG值 除255 放入相應(yīng)位置 如圖選擇了一塊粉色區(qū)域
/* 將UIView對(duì)象添加到父視圖上 */
[self.view addSubview:view];
結(jié)果如下圖
/* 添加/刪除子視圖方法總覽 */
[addSubview:]//添加子視圖
[insertSubview: atIndex:]//在指定的index處插入子視圖
[insertSubview: aboveSubview:]//在指定的視圖上添加子視圖
[insertSubview: belowSubview:]//在指定的視圖下添加子視圖
[bringSubviewToFront:]//把指定的子視圖移動(dòng)到最前面
[sendSubviewToBack:]//把指定的子視圖移動(dòng)到最后面
[exchangeSubviewAtIndex: withSubviewAtIndex:]//交換兩個(gè)指定位置的子視圖
removeFromSuperview//把子視圖從父視圖上移除
/* 關(guān)于UIView的屬性 */
1.backgroundColor 背景顏色
2.frame 位置及尺寸 以父視圖左上角為原點(diǎn)
3.center 中心點(diǎn)
4.hidden 布爾值 YES隱藏 No顯示
5.alpha 透明度
6.superview 本視圖的父視圖
7.subviews 本視圖的所有子視圖 是一個(gè)數(shù)組
8.tag 標(biāo)記
9.contentMode 內(nèi)容顯示模式 拉伸自適應(yīng)
10.layer
11.bounds 位置及尺寸 以自己的左上角為原點(diǎn) 影響其子視圖的位置布局
/* UIView對(duì)象的圓角設(shè)置 */
view.layer.masksToBounds = YES;
view.layer.cornerRadius = 5;//設(shè)置圓角半徑
view.layer.borderColor = [[UIColor cyanColor] CGColor];//設(shè)置邊框顏色
view.layer.borderWidth =3;//設(shè)置邊框?qū)挾?br>
結(jié)果 如下圖
二.UILabel
/* UILabel的屬性 */
1.text 文本內(nèi)容
2.frame 位置大小
3.textColor 字的顏色
4.textAlignment 對(duì)齊方式
(1)NSTextAlignmentLeft 左對(duì)齊
(2)NSTextAlignmentCenter 居中
(3)NSTextAlignmentRight 右對(duì)齊
5.numberOfLines 行數(shù)
6.lineBreakMode 斷行模式
(1)NSLineBreakByWordWrapping //以空格為邊界砰碴,保留單詞
(2)NSLineBreakByCharWrapping //保留整個(gè)字符
(3)NSLineBreakByClipping //簡(jiǎn)單剪裁靠欢,到邊界為止
(4)NSLineBreakByTruncatingHead //按照"……文字"顯示
(5)NSLineBreakByTruncatingTail //按照"文字……文字"顯示
(6)NSLineBreakByTruncatingMiddle //按照"文字……"顯示
7.layer 層級(jí)
8.font 字體大小
9.shadowColor 陰影顏色
10.shadowOffset 陰影偏移量 是CGSize 類型
11.alpha 透明度
/* UILabel的創(chuàng)建 */
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 120, 100, 40)];
label.backgroundColor = [UIColor yellowColor];//背景顏色
label.text = @"你好帥";//添加文本內(nèi)容
[self.view addSubview:label];//添加到父視圖上
label.textColor = [UIColor blueColor];//字體顏色設(shè)置
label.textAlignment = 1;//對(duì)齊方式 居中
label.layer.masksToBounds = YES;
label.layer.cornerRadius = 5;//圓角半徑
label.shadowColor = [UIColor redColor];//陰影顏色
label.shadowOffset = CGSizeMake(3, 3);//陰影偏移量
label.layer.borderWidth = 2;//邊框?qū)挾?
結(jié)果 如下圖
若我們這樣設(shè)置
label.text = @"你說什么 哦豁.what 嘻嘻我怎么聽不到";
label.numberOfLines = 0;
label.lineBreakMode = 2;
label.font = [UIFont systemFontOfSize:12];
就會(huì)變成下圖 保留了完整的字符
三. UIButton
/* UIButton的屬性 */
1.backgroundColor 背景顏色
2.frame 位置大小
3.titleLabel 標(biāo)題
4.layer 層級(jí)
5.alpha 透明度
/* button初始化 */
//有2種方式
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];// button上的字可以更改顏色
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];//button上的字顏色為白色 且不能更改
外觀控制屬性
[setImage: forState:] 添加圖片
[setBackgroundImage: forState:] 添加背景圖片
[setTitle: forState:] 添加標(biāo)題
[setTintColor:] 定義標(biāo)題字體顏色
[setTitleShadowColor: forState:] 標(biāo)題字體陰影顏色
在這里簡(jiǎn)單定義下屬性
[button setTitle:@"注冊(cè)" forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button1 setTitle:@"登錄" forState:UIControlStateNormal];
如下圖
當(dāng)然 你可以把它設(shè)置的很漂亮
如 透明底色帶邊框的button
這里小編是這么寫的
//注冊(cè)
UIButton *buttonOfSignIn = [UIButton buttonWithType:UIButtonTypeSystem];
buttonOfSignIn.frame = CGRectMake(10, 460, 355, 40);
buttonOfSignIn.backgroundColor = [UIColor colorWithRed:0.18 green:0.8 blue:0.58 alpha:1];
[imageView addSubview:buttonOfSignIn];
[buttonOfSignIn setTitle:@"注冊(cè)" forState:UIControlStateNormal];
[buttonOfSignIn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[buttonOfSignIn addTarget:self action:@selector(handleSignIn:) forControlEvents:UIControlEventTouchUpInside];
//返回登錄
UIView *viewOfGetBack = [[UIView alloc] initWithFrame:CGRectMake(10, 530, 350, 40)];
viewOfGetBack.backgroundColor = [UIColor colorWithRed:0.18 green:0.8 blue:0.58 alpha:1];
viewOfGetBack.alpha = 0.1;
[imageView addSubview:viewOfGetBack];
UIButton *buttonOfGetBack = [UIButton buttonWithType:UIButtonTypeSystem];
buttonOfGetBack.frame = CGRectMake(10, 530, 350, 40);
[buttonOfGetBack setTitle:@"返回登錄" forState:UIControlStateNormal];buttonOfGetBack.layer.masksToBounds = YES;
buttonOfGetBack.layer.borderWidth = 1;
buttonOfGetBack.layer.borderColor = [[UIColor colorWithRed:0.18 green:0.8 blue:0.58 alpha:1] CGColor];
buttonOfGetBack.titleLabel.font = [UIFont boldSystemFontOfSize:18];
[buttonOfGetBack setTitleColor:[UIColor colorWithRed:0.2 green:0.8 blue:0.58 alpha:1] forState:UIControlStateNormal];
[buttonOfGetBack addTarget:self action:@selector(handleGetBack:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:buttonOfGetBack];
四.UIImageView.UIImage
UIImageView是用于顯示圖片的類 UIImage是圖片類
使用ImageView設(shè)置動(dòng)態(tài)圖
//初始化圖片數(shù)組
NSMutableArray *array = [NSMutableArray array];
for (int i = 1; i < 19; i++) {
NSString *flowerName = [NSString stringWithFormat:@"flower%d.tiff", i];
[UIImage imageNamed:flowerName];
UIImage *flowerImage = [UIImage imageNamed:flowerName];
[array addObject:flowerImage];//將圖片用for循環(huán)放入數(shù)組中
}
imageView.animationImages = array;設(shè)置動(dòng)態(tài)圖片數(shù)組
imageView.animationDuration = 0.5;設(shè)置播放一組動(dòng)態(tài)圖片的時(shí)間
imageView.animationRepeatCount = 0;設(shè)置重復(fù)次數(shù) 0為無限重復(fù)
[_window addSubview:imageView];
[imageView startAnimating];開始動(dòng)畫
這樣向日葵就會(huì)左右搖動(dòng)啦QVQ
//UIImage圖片防止渲染的方法:
[UIImage imageNamed:@"1"] imageWithRenderingModem:UIImageRenderingModeAlwaysOriginall];
五.UITextField
UITextField 輸入框 是控制文本輸入和顯示的控件
textfield屬性:
1.text 文本內(nèi)容
2.textColor 文本字顏色
3.textAlignment 對(duì)齊方式
4.font 字大小
5.frame 位置大小
6.placeholder 占位符
7.leftView 左視圖
8.rightView 右視圖
9.borderStyle 邊框風(fēng)格
(1)UITextBorderStyleNone默認(rèn)無
(2)UITextBorderStyleRoundedRect圓角
(3)UITextBorderStyleLine邊緣線
(4)UITextBorderStyleBezel貝塞爾
10.leftViewMode 左視圖顯示模式
11.rightViewMode 右視圖顯示模式
12.clearButtonMode 清除按鈕模式
13.inputAccessoryView 輸入視圖上方的輔助視圖
14.inputView 自定義輸入鍵盤
15.returnKeyType 鍵盤右下角return 按鍵類型
16.keyboardType 彈出的鍵盤類型
17.secureTextEntry 是否密文輸入
//點(diǎn)擊return 回收鍵盤
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField canResignFirstResponder];//
[textField endEditing:YES];
return YES;
}
//設(shè)置placeholder的字體顏色
textOfUser.placeholder = @"請(qǐng)輸入已驗(yàn)證手機(jī)號(hào)碼或郵箱";
NSMutableDictionary *dict = [NSMutableDictionary dictionary];//新建可變字典
dict[NSForegroundColorAttributeName] = [UIColor lightGrayColor];//設(shè)置顏色屬性
NSAttributedString *attribute = [[NSAttributedString alloc] initWithString:textOfUser.placeholder attributes:dict];
[textOfUser setAttributedPlaceholder:attribute];
默認(rèn)占位符文字是黑色的 這里改成了灰色~
UITextField 協(xié)議
簽訂協(xié)議后 可以在其聲明周期中進(jìn)行代碼編寫
生命周期:
//開始編輯前 判斷能否編輯
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return YES;
}
已經(jīng)開始編輯時(shí) - (void)textFieldDidBeginEditing:(UITextField *)textField
{
}
判斷是否結(jié)束編輯 - (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
return YES;
}
點(diǎn)擊return回收鍵盤 - (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField endEditing:YES];
return YES;
}