iOS学习日记(第二天)

来源:互联网 发布:nero软件 编辑:程序博客网 时间:2024/06/07 03:50

使用代码设置Lable及简单属性

// 1.创建UILabel对象UILabel *lable = [[UILabel alloc] init];// 2.设置label的framelable.frame = CGRectMake(100, 100, 175, 175);// 3.设置label的背景lable.backgroundColor = [UIColor greenColor];// 4.设置label的文字lable.text = @"颜色";// 5.设置文字居中lable.textAlignment = NSTextAlignmentCenter;// 6.设置文字的大小lable.font = [UIFont systemFontOfSize:24];// 7.设置文字的颜色lable.textColor = [UIColor redColor];// 8.设置阴影lable.shadowOffset = CGSizeMake(10, 20);lable.shadowColor = [UIColor blackColor];// 9.设置省略号的位置lable.lineBreakMode = NSLineBreakByTruncatingMiddle;// 10.设置行数lable.numberOfLines = 2;// 11.将label对象添加到控制器的View中[self.view addSubview:lable];

使用代码设置ImageView及简单属性

// 1.创建UIImageView对象UIImageView *imageView = [[UIImageView alloc] init];// 2.设置frame-->位置和尺寸imageView.frame = CGRectMake(100, 100, 175, 175);// 3.设置背景颜色imageView.backgroundColor = [UIColor blackColor];// 4.设置显示的图片(三种)   (1) imageView.image = [UIImage imageNamed:@"1"];   (2) NSString *string = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"png"];   (3) imageView.image = [UIImage imageWithContentsOfFile:string];// 5.设置图片的内容模式imageView.contentMode = UIViewContentModeScaleToFill;// 6.加载图片[self.view addSubview:imageView];

contentMode属性

  • 带有scale单词的:图片有可能会拉伸

    • UIViewContentModeScaleToFill
      • 将图片拉伸至填充整个imageView
      • 图片显示的尺寸跟imageView的尺寸是一样的
    • 带有aspect单词的:保持图片原来的宽高比
      • UIViewContentModeScaleAspectFit
        • 保证刚好能看到图片的全部
      • UIViewContentModeScaleAspectFill
        • 拉伸至图片的宽度或者高度跟imageView一样
  • 没有scale单词的:图片绝对不会被拉伸,保持图片的原尺寸

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

设置frame

// 第一种方法:直接设置 imageView.frame = CGRectMake(100, 100, 175, 175);//第二种方法:根据图片设置(直接传入图片大小)    NSString *string = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"png"];    UIImage *image =[UIImage imageWithContentsOfFile:string];    imageView.frame = CGRectMake(100, 100, image.size.width, image.size.height);//第三种方法(initWithFrame)    UIImageView *imageView = [UIImageView alloc] initWithFrame:CGRectMake(100, 100, 175, 175);//第四种方法(initWithImage)    NSString *string = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"png"];    UIImage *image = [UIImage imageWithContentsOfFile:string];    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];//  注意:如果是通过initWithImage创建UIImageView的对象,该对象的尺寸,就等于image的尺寸

小语法点

  • 不能直接修改:OC对象的结构体属性的成员
  • 下面的写法是错误的
imageView.frame.size = imageView.image.size;
  • 正确写法
CGRect tempFrame = imageView.frame;tempFrame.size = imageView.image.size;imageView.frame = tempFrame;

修改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];
0 0