当前位置 博文首页 > cungudafa的博客:iOS基础控件常用属性

    cungudafa的博客:iOS基础控件常用属性

    作者:[db:作者] 时间:2021-09-08 10:28

    UIButton

    [btn setBackgroundImage:[UIImage imageNamed:@"icon"] forState:UIControlStateNormal];
    [btn seBackgroundColor: [UIColor blueColor]];
    
    [btn setTitle:@"title" forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize: 14.0];
    [btn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
    //设置标题偏移量
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
    [btn setImageEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
    [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    

    UILabel

    label.text = @”文本信息”;
    lable.textColor = [UIColor redColor];
    label.textAlignment = NSTextAlignmentLeft;
    label.lineBreakMode = NSLineBreakByCharWrapping;//换行
    label.shadowColor = [UIColor greenColor];
    //设置文本投影位置(与系统坐标一致)
    label.shadowOffset = CGSizeMake(0, 5);
    label.font = [UIFont systemFontOfSize:20];//字号
    label.font = [UIFont boldSystemFontOfSize:20]; //加粗
    label.font = [UIFont fontWithName:@"Arial" size:16]; //指定
    label.textAlignment = NSTextAlignmentCenter; //还有NSTextAlignmentLeft、 NSTextAlignmentRight.
    label.numberOfLines = 0;
    //圆角
    label.layer.cornerRadius = 5;
    label.clipsToBounds = YES;
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"这只是一个文本而已,没有其他的作用。这只是一个文本而已,没有其他的作用。这只是一个文本而已,没有其他的作用。这只是一个文本而已,没有其他的作用"];
    
    //设置指定区域文本的字体大小
    [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, 10)];
    [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(11, str.length-11)];
    //设置指定区域文本的字体颜色
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 20)];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(21, str.length-21)];
    //设置指定区域文本的字间距
    [str addAttribute:NSKernAttributeName value:@3 range:NSMakeRange(0, 15)];
    [str addAttribute:NSKernAttributeName value:@5 range:NSMakeRange(15, str.length-15)];
    

    UIImageView

    self.imageView.image = [UIImage imageNamed:@"demo"];
    
    cs