03 常用UI控件


//UILabel 文本标签
    UILabel *txtLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 200, 50)];
    txtLabel.backgroundColor = [UIColor grayColor];
    //文本
    txtLabel.text = @"31班";
    //字体
    txtLabel.font = [UIFont systemFontOfSize:20];
    //加粗字体
    txtLabel.font = [UIFont boldSystemFontOfSize:17];
//    [UIFont italicSystemFontOfSize:20];
    //居中显示
    txtLabel.textAlignment = NSTextAlignmentCenter;
    
    //几行,如果为0就是自动换行
    txtLabel.numberOfLines = 0;
    
    //颜色
    txtLabel.textColor = [UIColor redColor];
    //阴影颜色
    txtLabel.shadowColor = [UIColor greenColor];
    //阴影的偏移量
    txtLabel.shadowOffset = CGSizeMake(2, 2);
    
    //设置自适应文本
    [txtLabel sizeToFit];
    
    [self.view addSubview:txtLabel];


常用属性:
例子:


//UIImage 图片对象
    //png bmp jpeg(jpg, jpg2000) pdf
    UIImage *img = [UIImage imageNamed:@"checkbox_empty.png"];
    
    
//    UIImageView 图片视图
    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
    imgView.backgroundColor = [UIColor greenColor];
    imgView.frame = CGRectMake(10, 50, 100, 100);
    
    //image view的填充模式
    imgView.contentMode = UIViewContentModeCenter;
    
    //响应用户触摸
    imgView.userInteractionEnabled = YES;
    
    //高亮状态下的图片
    imgView.highlightedImage = [UIImage imageNamed:@"checkbox_full.png"];
    
    //设置高亮状态
    imgView.highlighted = YES;
    
    [self.view addSubview:imgView];
    
    
    //动画图片
    NSMutableArray *imgArray = [[NSMutableArray alloc] initWithCapacity:21];
    for (int i = 0; i < 21; i++) {
        NSString *imgName = [NSString stringWithFormat:@"%d.jpg", i+1];
        UIImage *img = [UIImage imageNamed:imgName];
        //可变数组不能添加nil对象
        [imgArray addObject:img];
    }
    
    UIImageView *animatedView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, 395, 207)];
    [self.view addSubview:animatedView];
    
    //设置动画数组给UIImageView
    animatedView.animationImages = imgArray;
    //动画总共持续的时间
    animatedView.animationDuration = 2;
    //动画持续的次数,默认是0,0表示无穷
    animatedView.animationRepeatCount = 2;
    
    //开始动画
    [animatedView startAnimating];


1. UIImage的常用属性:
1.1 UIImage的常用方法:


2. UIImageView的常用属性:
2.1 UIImageView的常用方法:


1. UIControl的使用
    UIControl是所有具有事件处理控件的父类。
    控件主要响应3种事件:基于触摸的事件,基于值的事件,基于编辑的事件。
    常用事件:
                    如按钮的点击事件:UIControlEventTouchUpInside
                    进度条拖动:UIControlEventValueChanged
     
    

2. UIControl常用方法:




:



 //UITextField
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(30, 20, 100, 20)];
    
//    textField.backgroundColor = [UIColor purpleColor];
    
    [self.view addSubview:textField];
    
    //常用属性
    //边框
    textField.borderStyle = UITextBorderStyleNone;
    
    textField.font = [UIFont italicSystemFontOfSize:13];
    textField.textColor = [UIColor redColor];
    //对齐的方式
    textField.textAlignment = NSTextAlignmentCenter;
    
//    textField.text = @"sdfghjk";
    
    //提示文字
    textField.placeholder = @"请输入邮箱";
    //安全输入
    textField.secureTextEntry = YES;
    
    
//    textField.keyboardType = UIKeyboardTypePhonePad;
    
    textField.clearButtonMode = UITextFieldViewModeAlways;
    
    //UITextView
    
    textField.delegate = self;
    
}

//text field 的代理方法

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSLog(@"已经开始编辑,键盘已经弹出的时候调用");
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    NSLog(@"将要开始编辑,键盘弹出之前");
    return YES;
}

//键盘将要收回
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    return YES;
}

//键盘已经收回
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    
}

//TODO:作业
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    return YES;
}


- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    //可以收回键盘
    [textField resignFirstResponder];
    
    return YES;
}













//滑块
    self.wxhlSlider.minimumValue = 0;
    self.wxhlSlider.maximumValue = 1;
    
    self.wxhlSlider.value = 0.2;
    
    [self.wxhlSlider addTarget:self
                        action:@selector(sliderAction:)
              forControlEvents:UIControlEventValueChanged];
    
    //分段控件
    NSArray *items = @[@"消息", @"通话"];
    UISegmentedControl *mySegCtrl = [[UISegmentedControl alloc] initWithItems:items];
    
    
    
    [self.wxhlSegCtrl setTitle:@"消息" forSegmentAtIndex:0];
    [self.wxhlSegCtrl setTitle:@"通话" forSegmentAtIndex:1];
    //设置选中的按钮
    self.wxhlSegCtrl.selectedSegmentIndex = 1;
    
    [self.wxhlSegCtrl addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    
    
    //加载等待视图
    [self.wxhlActivityView startAnimating];
    
    [self.wxhlActivityView stopAnimating];
    
    self.wxhlActivityView.hidesWhenStopped = YES;
    
    //PageControl
    self.wxhlPageCtrl.numberOfPages = 5;
    self.wxhlPageCtrl.currentPage = 3;
    
    //UIStepper
    [self.wxhlStepper addTarget:self action:@selector(stepperAction) forControlEvents:UIControlEventValueChanged];
//    self.wxhlStepper.autorepeat = NO;
    self.wxhlStepper.wraps = YES;
    
}

- (void)stepperAction
{
    NSLog(@"步进:%f", self.wxhlStepper.value);
}

- (void)segAction:(id)sender
{
    
}

- (void)sliderAction:(UISlider *)sender
{
//    NSLog(@"slider value = %ld", self.wxhlSlider.value);
    NSLog(@"slider value = %f", sender.value);
}













  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值