基础控件 UILabel UIButton UITextField UIImageView

UILabel:显示文本的控件

UILabel属性设置

        _label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];//初始化
        _label.text = @"用户名";//要显示的文本内容
        _label.textColor = [UIColor redColor];//文本内容的颜色
        _label.textAlignment = NSTextAlignmentLeft;//文本的对齐方式(水平方向)
        /*对齐方式有三种:
         NSTextAlignmentLeft  居左
         NSTextAlignmentCenter  居中
         NSTextAlignmentRight  居右
        */
        _label.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];//黑色加粗,20号字体
        _label.numberOfLines = 3;//行数  显示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";//要显示的文本内容
        _textField.backgroundColor = [UIColor lightGrayColor];//背景颜色
        _textField.textColor = [UIColor greenColor];//文本内容的颜色
        _textField.textAlignment = NSTextAlignmentCenter;//文本的对齐方式(水平方向)
        /*对齐方式有三种:
         NSTextAlignmentLeft  居左
         NSTextAlignmentCenter  居中
         NSTextAlignmentRight  居右
         */
        _textField.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];//黑体加粗 20号
        _textField.placeholder = @"你好";//占位字符串
        _textField.enabled = YES;//是否允许修改
        _textField.clearsOnBeginEditing = YES;//是否开始输入的时候,清空输入框的内容
        _textField.secureTextEntry = NO;//是否文字以圆点显示  YES 为密码模式  NO为普通模式
//        _textField.keyboardType = UIKeyboardTypeNumberPad;//数字键盘
        /*键盘样式:
         
         */
//        _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已经编辑的时候告诉委托人
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    NSLog(@"已经开始编辑");
}
//当textField将要完成编辑的时候告诉委托人
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    NSLog(@"将要完成编辑");
    return YES;
}
//当textField已经完成编辑的时候告诉委托人
- (void)textFieldDidEndEditing:(UITextField *)textField {
    NSLog(@"%@",textField.text);
    NSLog(@"已经完成编辑");
}
//点击键盘回车按键的时候告诉委托人
- (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];//设置指定状态下的标题
        NSString *title = [_button titleForState:UIControlStateNormal];//获取指定状态下的标题
        NSLog(@"%@", title);
        [_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];//设置执行状态下的标题颜色
        UIColor *titleColor = [_button titleColorForState:UIControlStateNormal];//获取指定状态下的标题颜色
        NSLog(@"%@", titleColor);
        [_button setTitleShadowColor:[UIColor purpleColor] forState:UIControlStateNormal];//设置指定状态下标题阴影颜色
        UIColor *shadowColor = [_button titleShadowColorForState:UIControlStateNormal];//获取指定状态下标题阴影颜色
        UIImage *image = [UIImage imageNamed:@"novel"];
        [_button setImage:image forState:UIControlStateNormal];//设置指定状态下的前景图片
        NSLog(@"%@",image);
        UIImage *imageForNormal =[_button imageForState:UIControlStateNormal];//获取指定状态下的前景图片
        NSLog(@"%@", imageForNormal);
        //设置指定状态下的背景图片
        [_button setBackgroundImage:image forState:UIControlStateNormal];
        UIImage *imageForBackground = [_button backgroundImageForState:UIControlStateNormal];//获取指定状态下的背景图片
        NSLog(@"%@", imageForBackground);

添加点击事件

        //添加点击事件  
        [_button addTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventTouchUpInside];
        
        //移除按钮的点击事件
//        [_button removeTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventTouchUpInside];
//button回调方法
  - (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];//一组动态图片
        for (int i = 0; i < 30; i++) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"footLeft_%d.jpg", i]];
            [imageMArray addObject:image];
        }
        _imageView.animationImages = imageMArray;//设置一组动态图片
        _imageView.animationDuration = 3;//设置播放一组动态图片的事件
        _imageView.animationRepeatCount = 3;//设置重复次数
        [_imageView startAnimating];//开始动画
//        [_imageView stopAnimating];//结束动画

你可能感兴趣的:(基础控件 UILabel UIButton UITextField UIImageView)