iOS入门-UI基础控件

iOS入门-UI基础控件

UIlabel

  • UILabel建立
    UILabel *label=[[UILabel alloc] init];
    
  • UILabel基本设置
  // 设置label背景色
  label.backgroundColor=[UIColor redColor];
  // 设置label位置大小
  label.frame=CGRectMake(100, 100, 100, 300);
  // 设置label中显示文字
  label.text=@"hello world!";
  // label中国显示文字居中
  label.textAlignment=NSTextAlignmentCenter;
  // label中文字行数随文字数量自动改变
  label.numberOfLines=0;
  // 将label添加到父控件中
  [self.view addSubview:label];

UIImageView

  • UIImageView建立
    UIImageView *imageView=[[UIImageView alloc] init];
    
  • UIImageView基本设置

      // 设置imageView的位置与大小
      imageView.frame=CGRectMake(50, 100,300, 300);
      // 设置imageView的背景颜色
      imageView.backgroundColor=[UIColor redColor];
      // 加载图片
      imageView.image=[UIImage imageNamed:@"图片名称"];
      // 将图片置于控件底部
      imageView.contentMode=UIViewContentModeBottom;
      // 添加到父控件中
      [self.view addSubview:imageView];
    
    • contentMode属性
    • 带有scale单词的:图片有可能会拉伸
      • UIViewContentModeScaleToFill
        • 将图片拉伸至填充整个imageView
        • 图片显示的尺寸跟imageView的尺寸是一样的
      • 带有aspect单词的:保持图片原来的宽高比
        • UIViewContentModeScaleAspectFit : 保证刚好能看到图片的全部
        • UIViewContentModeScaleAspectFill :拉伸至图片的宽度或者高度跟imageView一样
  • 没有scale单词的:图片绝对不会被拉伸,保持图片的原尺寸

    • UIViewContentModeCenter
    • UIViewContentModeTop
    • UIViewContentModeBottom
    • UIViewContentModeLeft
    • UIViewContentModeRight
    • UIViewContentModeTopLeft
    • UIViewContentModeTopRight
    • UIViewContentModeBottomLeft
    • UIViewContentModeBottomRight

UIButton

  • UIButton建立
    UIButton *btn=[[UIButton alloc] init];
    
  • UIButton基本设置
      // 设置btn在正常状况下加载的图片
      [btn setImage:[UIImage imageNamed:@"图片名称"] forState:UIControlStateNormal];
      // 设置btn在高亮状态下加载的图片
      [btn setImage:[UIImage imageNamed:@"图片名称" ] forState:UIControlStateHighlighted];
      // 设置btn在正常状态下的背景图片
      [btn setBackgroundImage:[UIImage imageNamed:@"图片名称" ] forState:UIControlStateNormal];
      // 设置btn在高亮状态下的背景图片
      [btn setBackgroundImage:[UIImage imageNamed:@"图片名称"] forState:UIControlStateHighlighted];
      // 设置btn在正常状态下显示的文字
      [btn setTitle:@"点我啊" forState:UIControlStateNormal];
      // 设置btn在高亮状态下显示的文字
      [btn setTitle:@"神经病" forState:UIControlStateHighlighted];
      // 设置btn在正常状态下文字颜色
      [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
      // 设置btn位置与大小
      btn.frame=CGRectMake(100, 100, 100, 30);
      // 监听事件
      [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
      // 将btn添加到父控件中
      [self.view addSubview:btn];
    
  • 注意点
    • 不能直接修改:OC对象的结构体属性的成员
    • 下面的写法是错误的
imageView.frame.size = imageView.image.size;
  • 正确写法
CGRect tempFrame = imageView.frame;
tempFrame.size = imageView.image.size;
imageView.frame = tempFrame;
  • initWithImage:方法
  • 利用这个方法创建出来的imageView的尺寸和传入的图片尺寸一样

  • 修改frame的3种方式

  • 直接使用CGRectMake函数
imageView.frame = CGRectMake(100, 100, 200, 200);
  • 利用临时结构体变量
CGRect tempFrame = imageView.frame;
tempFrame.origin.x = 100;
tempFrame.origin.y = 100;
tempFrame.size.width = 200;
tempFrame.size.height = 200;
imageView.frame = tempFrame;
  • 使用大括号{}形式
imageView.frame = (CGRect){{100, 100}, {200, 200}};
  • 抽取重复代码

    • 将相同代码放到一个新的方法中
    • 不用的东西就变成方法的参数
  • 图片的加载方式

    • 有缓存
      UIImage *image = [UIImage imageNamed:@"图片名"];
      
      • 使用场合:图片比较小、使用频率较高
      • 建议把需要缓存的图片直接放到Images.xcassets
    • 无缓存
      NSString *file = [[NSBundle mainBundle] pathForResource:@"图片名" ofType:@"图片的扩展名"];
      UIImage *image = [UIImage imageWithContentsOfFile:@"图片文件的全路径"];
      
      • 使用场合:图片比较大、使用频率较小
      • 不需要缓存的图片不能放在Images.xcassets
    • 放在Images.xcassets里面的图片,只能通过图片名去加载图片
  • 延迟做一些事情

[abc performSelector:@selector(stand:) withObject:@"123" afterDelay:10];
// 10s后自动调用abc的stand:方法,并且传递@"123"参数
  • 音频文件的简单播放
// 创建一个音频文件的URL(URL就是文件路径对象)
NSURL *url = [[NSBundle mainBundle] URLForResource:@"音频文件名" withExtension:@"音频文件的扩展名"];
// 创建播放器
self.player = [AVPlayer playerWithURL:url];
// 播放
[self.player play];
  • 个人说明 
    • 看到这里说明你已经耐心的看完了本节内容,欢迎来到iOS开发的世界,相信经过你的努力,你一定会成为移动开发行业中的翘楚,加油吧!
    • 本人从事iOS开发差不多也有一年半的时间,虽然朋友都说我更适合做运营,但是我还是深深地爱着敲代码这个工作,因为我喜欢这种从无到有的过程。虽然对于技术大牛来说我还是小白,但是经过一段时间的开发工作,逐渐也有自己的一些心得体会,所以决定从今天开始,我会推出系列化的微博,首先就从基础开始吧!
    • 微博刚开始写,可能会存在很多不足,但是相信经过一段时间后,肯定会越来越好的,欢迎各位朋友提出建议,我也很希望能够与各位朋友交流,移动开发方面与互联网运营方面都可以。

欢迎加我个人微信,扫描下面二维码吧 
iOS入门-UI基础控件_第1张图片

你可能感兴趣的:(iOS入门-UI基础控件)