iOS UILabel换行同时修改字体大小颜色

来源:互联网 发布:java编写乘法表 编辑:程序博客网 时间:2024/05/16 17:25

    UIButton *onlyPriceBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    onlyPriceBtn.layer.borderColor = [HuConfigration uiColorFromString:@"#F0493D"].CGColor;;

    onlyPriceBtn.layer.borderWidth = 0.5f;

    onlyPriceBtn.layer.cornerRadius = 4;

    onlyPriceBtn.layer.masksToBounds = YES;

    onlyPriceBtn.frame = CGRectMake(15CGRectGetMaxY(titleLabel.frame)+15HHBWIDTH - 3060);

    [self.bgView addSubview:onlyPriceBtn];

    

    UILabel *btnTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0020060)];

    btnTitleLabel.center = onlyPriceBtn.center;

    btnTitleLabel.textColor = kHuColor(#A5A5A5);

    btnTitleLabel.font = [UIFont customFontSize:12];

    btnTitleLabel.numberOfLines = 0;

    btnTitleLabel.text = @"独享价30.0\n购买后仅供自己观看学习";//使用斜杠一定不要忘记写numberOfLines 否则无效

    btnTitleLabel.textAlignment = NSTextAlignmentCenter;

    NSMutableAttributedString *attributer = [[NSMutableAttributedStringalloc]initWithString:btnTitleLabel.text];

    

    [attributer addAttribute:NSForegroundColorAttributeName

                       value:kHuColor(#F0493D)

                       range:NSMakeRange(08)];

    [attributer addAttribute:NSFontAttributeName

                       value:[UIFont customFontSize:16]

                       range:NSMakeRange(08)];

 

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];//调整行间距

    [paragraphStyle setLineSpacing:4];

    [attributer addAttribute:NSParagraphStyleAttributeName

                       value:paragraphStyle

                       range:NSMakeRange(0, btnTitleLabel.text.length)];

    [paragraphStyle setAlignment:NSTextAlignmentCenter];//为了美观调整行间距,但是当调整行间距时上面设置的居中不能用看 所以要加这一句 [paragraphStyle setAlignment:NSTextAlignmentCenter] 不然没有居中效果

    btnTitleLabel.attributedText = attributer;

    [self.bgView addSubview:btnTitleLabel];

 

效果图


原创粉丝点击