[關于UIView.UILabel.UIButton.UIImageView.UITextField等子控件布局森研究]

一. UIView

/* UIView 的創建 */
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 100, 30)];

/* 設置UIView背景顏色 */
view.backgroundColor = [UIColor cyanColor];

/* 使用取色器自定義RBG顏色的方法 */
view.backgroundColor = [UIColor colorWithRed:255 / 255 green:102 / 255 blue:255 / 255 alpha:1];//找到你想要的顏色RBG值 除255 放入相應位置 如圖選擇了一塊粉色區域

選中一塊粉色

參照RGB圖表

/* 將UIView對象添加到父視圖上 */
[self.view addSubview:view];
結果如下圖


顯示結果

/* 添加/刪除子視圖方法總覽 */
[addSubview:]//添加子視圖
[insertSubview: atIndex:]//在指定的index處插入子視圖
[insertSubview: aboveSubview:]//在指定的視圖上添加子視圖
[insertSubview: belowSubview:]//在指定的視圖下添加子視圖
[bringSubviewToFront:]//把指定的子視圖移動到最前面
[sendSubviewToBack:]//把指定的子視圖移動到最后面
[exchangeSubviewAtIndex: withSubviewAtIndex:]//交換兩個指定位置的子視圖
removeFromSuperview//把子視圖從父視圖上移除

/* 關于UIView的屬性 */
1.backgroundColor 背景顏色
2.frame 位置及尺寸 以父視圖左上角為原點
3.center 中心點
4.hidden 布爾值 YES隱藏 No顯示
5.alpha 透明度
6.superview 本視圖的父視圖
7.subviews 本視圖的所有子視圖 是一個數組
8.tag 標記
9.contentMode 內容顯示模式 拉伸自適應
10.layer
11.bounds 位置及尺寸 以自己的左上角為原點 影響其子視圖的位置布局

/* UIView對象的圓角設置 */
view.layer.masksToBounds = YES;
view.layer.cornerRadius = 5;//設置圓角半徑
view.layer.borderColor = [[UIColor cyanColor] CGColor];//設置邊框顏色
view.layer.borderWidth =3;//設置邊框寬度
結果 如下圖

顯示結果

二.UILabel

/* UILabel的屬性 */
1.text 文本內容
2.frame 位置大小
3.textColor 字的顏色
4.textAlignment 對齊方式
 (1)NSTextAlignmentLeft 左對齊
 (2)NSTextAlignmentCenter 居中
 (3)NSTextAlignmentRight 右對齊
5.numberOfLines 行數 
6.lineBreakMode 斷行模式
 (1)NSLineBreakByWordWrapping //以空格為邊界,保留單詞
 (2)NSLineBreakByCharWrapping //保留整個字符
 (3)NSLineBreakByClipping //簡單剪裁,到邊界為止
 (4)NSLineBreakByTruncatingHead //按照"……文字"顯示
 (5)NSLineBreakByTruncatingTail //按照"文字……文字"顯示
 (6)NSLineBreakByTruncatingMiddle //按照"文字……"顯示
7.layer 層級
8.font 字體大小
9.shadowColor 陰影顏色
10.shadowOffset 陰影偏移量 是CGSize 類型
11.alpha 透明度

/* UILabel的創建 */
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 120, 100, 40)];
label.backgroundColor = [UIColor yellowColor];//背景顏色
label.text = @"你好帥";//添加文本內容
[self.view addSubview:label];//添加到父視圖上
label.textColor = [UIColor blueColor];//字體顏色設置
label.textAlignment = 1;//對齊方式 居中
label.layer.masksToBounds = YES;
label.layer.cornerRadius = 5;//圓角半徑
label.shadowColor = [UIColor redColor];//陰影顏色
label.shadowOffset = CGSizeMake(3, 3);//陰影偏移量
label.layer.borderWidth = 2;//邊框寬度

結果 如下圖



若我們這樣設置

label.text = @"你說什么 哦豁.what 嘻嘻我怎么聽不到";
label.numberOfLines = 0;
label.lineBreakMode = 2;
label.font = [UIFont systemFontOfSize:12];

就會變成下圖 保留了完整的字符


三. UIButton

/* UIButton的屬性 */
1.backgroundColor 背景顏色
2.frame 位置大小
3.titleLabel 標題
4.layer 層級
5.alpha 透明度

/* button初始化 */
//有2種方式
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];// button上的字可以更改顏色
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];//button上的字顏色為白色 且不能更改

外觀控制屬性
[setImage: forState:] 添加圖片
[setBackgroundImage: forState:] 添加背景圖片
[setTitle: forState:] 添加標題
[setTintColor:] 定義標題字體顏色
[setTitleShadowColor: forState:] 標題字體陰影顏色

在這里簡單定義下屬性
[button setTitle:@"注冊" forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button1 setTitle:@"登錄" forState:UIControlStateNormal];
如下圖

當然 你可以把它設置的很漂亮
如 透明底色帶邊框的button


這里小編是這么寫的
//注冊

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:@"注冊" 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設置動態圖

//初始化圖片數組
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循環放入數組中
}
imageView.animationImages = array;設置動態圖片數組
imageView.animationDuration = 0.5;設置播放一組動態圖片的時間
imageView.animationRepeatCount = 0;設置重復次數 0為無限重復
[_window addSubview:imageView];
[imageView startAnimating];開始動畫

這樣向日葵就會左右搖動啦QVQ

//UIImage圖片防止渲染的方法:
[UIImage imageNamed:@"1"] imageWithRenderingModem:UIImageRenderingModeAlwaysOriginall];

五.UITextField
UITextField 輸入框 是控制文本輸入和顯示的控件

textfield屬性:
1.text 文本內容
2.textColor 文本字顏色
3.textAlignment 對齊方式
4.font 字大小
5.frame 位置大小
6.placeholder 占位符
7.leftView 左視圖
8.rightView 右視圖
9.borderStyle 邊框風格
  (1)UITextBorderStyleNone默認無                             
  (2)UITextBorderStyleRoundedRect圓角   
  (3)UITextBorderStyleLine邊緣線  
  (4)UITextBorderStyleBezel貝塞爾
10.leftViewMode 左視圖顯示模式
11.rightViewMode 右視圖顯示模式
12.clearButtonMode 清除按鈕模式
13.inputAccessoryView 輸入視圖上方的輔助視圖
14.inputView 自定義輸入鍵盤
15.returnKeyType 鍵盤右下角return 按鍵類型
16.keyboardType 彈出的鍵盤類型
17.secureTextEntry 是否密文輸入

//點擊return 回收鍵盤
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField canResignFirstResponder];//
    [textField endEditing:YES];
    return YES;
}

//設置placeholder的字體顏色
textOfUser.placeholder = @"請輸入已驗證手機號碼或郵箱";
NSMutableDictionary *dict = [NSMutableDictionary dictionary];//新建可變字典
dict[NSForegroundColorAttributeName] = [UIColor lightGrayColor];//設置顏色屬性
NSAttributedString *attribute = [[NSAttributedString alloc] initWithString:textOfUser.placeholder attributes:dict];
[textOfUser setAttributedPlaceholder:attribute];

默認占位符文字是黑色的 這里改成了灰色~


UITextField 協議
簽訂協議后 可以在其聲明周期中進行代碼編寫
生命周期:
//開始編輯前 判斷能否編輯

  • (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
    {
    return YES;
    }
    已經開始編輯時
  • (void)textFieldDidBeginEditing:(UITextField *)textField
    {
    }
    判斷是否結束編輯
  • (BOOL)textFieldShouldEndEditing:(UITextField *)textField
    {
    return YES;
    }
    點擊return回收鍵盤
  • (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
    [textField endEditing:YES];
    return YES;
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容