UI控件(Label、ImageView、Button)的基本使用

UIlabel在storyboard中的使用

 

UILabel在代码中的使用

//    1、创建一个Label
    UILabel *label = [[UILabel alloc] init];
//    2、设置位置和尺寸
    label.frame = CGRectMake(100, 100, 175, 250);
//    3、设置内容
    label.text = @"德玛西亚 德玛西亚 德玛西亚 德玛西亚 德玛西亚 德玛西亚 德玛西亚 德玛西亚 德玛西亚 德玛西亚 德玛西亚 德玛西亚 德玛西亚 德玛西亚 德玛西亚 德玛西亚 德玛西亚 德玛西亚 德玛西亚 德玛西亚 德玛西亚 德玛西亚 德玛西亚 德玛西亚 德玛西亚";
//    4、设置字体颜色
    label.textColor = [UIColor blueColor];
//    5、设置字体大小
//    label.font = [UIFont systemFontOfSize:13.0];
//    label.font = [UIFont boldSystemFontOfSize:14.0];
    label.font = [UIFont italicSystemFontOfSize:15.0];
//    6、设置背景颜色
    label.backgroundColor = [UIColor purpleColor];
//    7、设置行数
    label.numberOfLines = 0;
//    8、设置阴影
//    8.1、设置阴影颜色
    label.shadowColor = [UIColor blackColor];
//    8.2、设置阴影偏移量
    label.shadowOffset = CGSizeMake(0, -3);
//    11、设置文字居中
    label.textAlignment = NSTextAlignmentCenter;
//    12、省略号的位置或剪切,字符串或者单词完整性
//    label.lineBreakMode = NSLineBreakByCharWrapping;
    label.lineBreakMode = NSLineBreakByTruncatingTail;
//    将Label添加到View当中
    [self.view addSubview:label];

 

UIImageview在storyboard中的使用

UIImageview在代码中的使用

在代码中的使用
//    1、创建UIImageView对象
    UIImageView *imageView = [[UIImageView alloc] init];
//    2、设置frame
//    imageView.frame = CGRectMake(100, 100, 175, 200);
    imageView.frame = self.view.bounds;
//    3、设置背景颜色
    imageView.backgroundColor = [UIColor purpleColor];
//    4、设置背景图片
    imageView.image = [UIImage imageNamed:@"1"];
//    5、设置内容模式
   
     UIViewContentModeRedraw, 重新绘制 drawRect
     
     // 带Scale比例--图片之后可能会被缩放
     UIViewContentModeScaleToFill, // 默认情况 : 压缩或者拉伸图片,让图片可以填充整个控件
     
     UIViewContentModeScaleAspectFit, // 宽度比例不变 : 图片可以被拉伸也可与被压缩,但是保持宽高比.Fit:适应,一部分填充
     UIViewContentModeScaleAspectFill,  // 宽度比例不变 : 图片可以被拉伸也可与被压缩,但是保持宽高比.Fill:填充
     
     // 图片不会被拉伸和压缩
     UIViewContentModeCenter,
     UIViewContentModeTop,
     UIViewContentModeBottom,
     UIViewContentModeLeft,
     UIViewContentModeRight,
     
     UIViewContentModeTopLeft,
     UIViewContentModeTopRight,
     UIViewContentModeBottomLeft,
     UIViewContentModeBottomRight,
     
    imageView.contentMode = UIViewContentModeScaleAspectFit;
//    6、添加毛玻璃效果
//    6.1创建UIToolbar对象
    UIToolbar *toolbar = [[UIToolbar alloc] init];
//    6.2设置toolbar的样式
    toolbar.barStyle = UIBarStyleBlack;
//    6.3设置toolbar的frame
    toolbar.frame = self.view.bounds;
//    6.4将toolbar添加到imageView中
    [imageView addSubview:toolbar];
//    7、将imageView添加到控制器View中
    [self.view addSubview:imageView];
    */





设置frame的四种方式

    //    1.创建一个uiImageView对象
//    UIImageView *uiImageView = [[UIImageView alloc] init];
    //    2.设置frame
    //    2.1最基本的frame设置
//    uiImageView.frame = CGRectMake(100, 100, 175, 250);
    //        设置图片
//    uiImageView.image = [UIImage imageNamed:@"2"];
    
    //    2.2根据图片大小来设置frame(掌握)
//    加载图片
//    UIImage *image = [UIImage imageNamed:@"2"];
//    设置frame
//    uiImageView.frame = CGRectMake(0, 100, image.size.width, image.size.height);
//    设置图片
//    uiImageView.image = image;
    //    2.3创建UIImageView对象时直接设置frame
//    UIImageView *uiImageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 175, 250)];
//    uiImageView.image = [UIImage imageNamed:@"1"];
    //    2.4创建UIImageView对象时直接设置图片(掌握)
    UIImageView *uiImageView = [[UIImageView alloc]initWithImage :
                                [UIImage imageNamed:@"1"]];
    //    改变位置   正好是view的中心位置
    uiImageView.center = CGPointMake(
                                     self.view.frame.size.width * 0.5,
                                     self.view.frame.size.height *0.5);
//    超出ImageView的部分jianqie
    uiImageView.clipsToBounds = YES;
    //    3.将UIImageView添加到控制器的View中
    [self.view addSubview:uiImageView];
}

 

UIButton在storyboard中的使用

UIButton在代码中的使用

- (void)viewDidLoad {
    [super viewDidLoad];
//创建一个Button对象
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    
//    1、设置frame
    btn.frame = CGRectMake(100, 500, 175, 75);
    
//    2、设置内容
//    2.1设置文字
    [btn setTitle:@"来点我啊" forState: UIControlStateNormal];
    [btn setTitle:@"点你大爷" forState: UIControlStateHighlighted];
    
//    2.1.1设置文字颜色
    [btn setTitleColor:[UIColor blueColor]
              forState:UIControlStateNormal];
    
    [btn setTitleColor:[UIColor redColor]
              forState:UIControlStateHighlighted];
    
//    2.2设置图片
    [btn setImage:[UIImage imageNamed: @"player_btn_pause_normal"]
         forState: UIControlStateNormal];
    
    [btn setImage:[UIImage imageNamed: @"player_btn_pause_highlight"]
         forState: UIControlStateHighlighted];
    
//    2.3设置背景图片
    [btn setBackgroundImage:[UIImage imageNamed:@"buttongreen"]
                   forState:UIControlStateNormal];
    
    [btn setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighted"]
                   forState:UIControlStateHighlighted];
    
//    3、添加到View中
    [self.view addSubview:btn];
    
    
    // 重要:代码创建的按钮,监听点击
    // Target : 目标,谁想做事情.
    // action : 要执行的方法(发送消息)
    // Events : touchUpInside
    // SEL sel = ;
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)btnClick:(UIButton *)btn
{
    NSLog(@"%@", btn);
}
- (void)demo
{
    NSLog(@"%s", __func__);
}

 

posted @ 2015-11-26 21:10  a滴答  阅读(358)  评论(0编辑  收藏  举报