基礎控件 UILabel UIButton UITextField UIImageView

UILabel:顯示文本的控件

UILabel屬性設置

        _label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];//初始化
        _label.text = @"用戶名";//要顯示的文本內(nèi)容
        _label.textColor = [UIColor redColor];//文本內(nèi)容的顏色
        _label.textAlignment = NSTextAlignmentLeft;//文本的對齊方式(水平方向)
        /*對齊方式有三種:
         NSTextAlignmentLeft  居左
         NSTextAlignmentCenter  居中
         NSTextAlignmentRight  居右
        */
        _label.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];//黑色加粗,20號字體
        _label.numberOfLines = 3;//行數(shù)  顯示3行,注意label的高度要能容納3行。如果3行沒能顯示完信息,沒顯示的信息以省略號代替。
        _label.lineBreakMode = NSLineBreakByCharWrapping;//斷行模式  以單詞為單位斷行
        _label.shadowColor = [UIColor yellowColor];//陰影顏色
        _label.shadowOffset = CGSizeMake(2, 1);//陰影大小   陰影向x正方向偏移2,向y正方向偏移1

UITextField :輸入框,是控制文本輸入和顯示的控件

UITextField屬性設置

     _textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 80, 200, 50)];
//        _textField.text = @"lanoukeji";//要顯示的文本內(nèi)容
        _textField.backgroundColor = [UIColor lightGrayColor];//背景顏色
        _textField.textColor = [UIColor greenColor];//文本內(nèi)容的顏色
        _textField.textAlignment = NSTextAlignmentCenter;//文本的對齊方式(水平方向)
        /*對齊方式有三種:
         NSTextAlignmentLeft  居左
         NSTextAlignmentCenter  居中
         NSTextAlignmentRight  居右
         */
        _textField.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];//黑體加粗 20號
        _textField.placeholder = @"你好";//占位字符串
        _textField.enabled = YES;//是否允許修改
        _textField.clearsOnBeginEditing = YES;//是否開始輸入的時候,清空輸入框的內(nèi)容
        _textField.secureTextEntry = NO;//是否文字以圓點顯示  YES 為密碼模式  NO為普通模式
//        _textField.keyboardType = UIKeyboardTypeNumberPad;//數(shù)字鍵盤
        /*鍵盤樣式:
         
         */
//        _textField.returnKeyType = UIReturnKeyGoogle;//鍵盤右下角return按鈕類型(枚舉值)
        /*return按鍵類型:
         UIReturnKeyDefault,
         UIReturnKeyGo,
         UIReturnKeyGoogle,
         UIReturnKeyJoin,
         UIReturnKeyNext,
         UIReturnKeyRoute,
         UIReturnKeySearch,
         UIReturnKeySend,
         UIReturnKeyYahoo,
         UIReturnKeyDone,
         UIReturnKeyEmergencyCall,
         UIReturnKeyContinue
         */
//        _textField.inputView = myInputView;//自定時輸入視圖  默認是鍵盤=>重寫鍵盤
//        _textField.inputAccessoryView = myAccessoryView;//輸入視圖上方的輔助視圖,默認是nil
        _textField.borderStyle = UITextBorderStyleRoundedRect;//邊框樣式(枚舉值)
        /*
         UITextBorderStyleNone, //無
         UITextBorderStyleLine,  顯示邊框外線條
         UITextBorderStyleBezel,  也是線條
         UITextBorderStyleRoundedRect  圓角
         */
        _textField.clearButtonMode = UITextFieldViewModeAlways;//清除按鈕模式
        /*
         UITextFieldViewModeNever,
         UITextFieldViewModeWhileEditing,
         UITextFieldViewModeUnlessEditing,
         UITextFieldViewModeAlways
         */
        UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
        leftView.backgroundColor = [UIColor redColor];
        _textField.leftView = leftView;//輸入框左視圖
        _textField.leftViewMode = UITextFieldViewModeAlways;//左視圖的顯示模式
        /*
         UITextFieldViewModeNever,
         UITextFieldViewModeWhileEditing,
         UITextFieldViewModeUnlessEditing,
         UITextFieldViewModeAlways
         */
//        _textField.rightView = rightView;//輸入框右視圖
        _textField.rightViewMode = UITextFieldViewModeAlways;//右視圖顯示模式
        //代理
        _textField.delegate = self;

代理方法

//當textFiel將要開始編輯的時候告訴委托人
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    NSLog(@"將要開始編輯");
    return YES;
}
//當textfield已經(jīng)編輯的時候告訴委托人
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    NSLog(@"已經(jīng)開始編輯");
}
//當textField將要完成編輯的時候告訴委托人
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    NSLog(@"將要完成編輯");
    return YES;
}
//當textField已經(jīng)完成編輯的時候告訴委托人
- (void)textFieldDidEndEditing:(UITextField *)textField {
    NSLog(@"%@",textField.text);
    NSLog(@"已經(jīng)完成編輯");
}
//點擊鍵盤回車按鍵的時候告訴委托人
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    //取消第一響應者
    NSLog(@"點擊return");
    [self.textField resignFirstResponder];
    return YES;
}

UIButton:按鈕

UIbutton屬性

        _button = [UIButton buttonWithType:UIButtonTypeSystem];
        _button.frame = CGRectMake(20, 150, 100, 40);
        _button.backgroundColor = [UIColor yellowColor];//背景顏色
        [_button setTitle:@"按鈕" forState:UIControlStateNormal];//設置指定狀態(tài)下的標題
        NSString *title = [_button titleForState:UIControlStateNormal];//獲取指定狀態(tài)下的標題
        NSLog(@"%@", title);
        [_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];//設置執(zhí)行狀態(tài)下的標題顏色
        UIColor *titleColor = [_button titleColorForState:UIControlStateNormal];//獲取指定狀態(tài)下的標題顏色
        NSLog(@"%@", titleColor);
        [_button setTitleShadowColor:[UIColor purpleColor] forState:UIControlStateNormal];//設置指定狀態(tài)下標題陰影顏色
        UIColor *shadowColor = [_button titleShadowColorForState:UIControlStateNormal];//獲取指定狀態(tài)下標題陰影顏色
        UIImage *image = [UIImage imageNamed:@"novel"];
        [_button setImage:image forState:UIControlStateNormal];//設置指定狀態(tài)下的前景圖片
        NSLog(@"%@",image);
        UIImage *imageForNormal =[_button imageForState:UIControlStateNormal];//獲取指定狀態(tài)下的前景圖片
        NSLog(@"%@", imageForNormal);
        //設置指定狀態(tài)下的背景圖片
        [_button setBackgroundImage:image forState:UIControlStateNormal];
        UIImage *imageForBackground = [_button backgroundImageForState:UIControlStateNormal];//獲取指定狀態(tài)下的背景圖片
        NSLog(@"%@", imageForBackground);

添加點擊事件

        //添加點擊事件  
        [_button addTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventTouchUpInside];
        
        //移除按鈕的點擊事件
//        [_button removeTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventTouchUpInside];
//button回調(diào)方法
  - (void)changeColor:(UIButton *)sender {
    sender.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:arc4random() % 256 / 255.0];
}

UIImageView:圖片顯示框

UIImageView的屬性

        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 100, 100)];
        _imageView.backgroundColor = [UIColor lightGrayColor];
       NSString *path = [[NSBundle mainBundle] pathForResource:@"footLeft_0" ofType:@"jpg"];
        UIImage *image = [UIImage imageWithContentsOfFile:path];
        _imageView.image = image;
//        [_imageView setImage:image];

動畫

//動畫
        NSMutableArray *imageMArray = [[NSMutableArray alloc] init];//一組動態(tài)圖片
        for (int i = 0; i < 30; i++) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"footLeft_%d.jpg", i]];
            [imageMArray addObject:image];
        }
        _imageView.animationImages = imageMArray;//設置一組動態(tài)圖片
        _imageView.animationDuration = 3;//設置播放一組動態(tài)圖片的事件
        _imageView.animationRepeatCount = 3;//設置重復次數(shù)
        [_imageView startAnimating];//開始動畫
//        [_imageView stopAnimating];//結束動畫

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容