首页 > 代码库 > iOS UILabel换行同时修改字体大小颜色

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

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #3e1e81 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; min-height: 16.0px } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #d12f1b } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #703daa } span.s1 { color: #000000 } span.s2 { color: #703daa } span.s3 { } span.s4 { color: #4f8187 } span.s5 { color: #31595d } span.s6 { color: #d12f1b } span.s7 { color: #272ad8 } span.s8 { color: #ba2da2 } span.s9 { color: #3e1e81 } span.s10 { color: #78492a } span.s11 { font: 14.0px "PingFang SC" } span.s12 { color: #008400 } span.s13 { font: 14.0px "PingFang SC"; color: #008400 }</style>

    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(15, CGRectGetMaxY(titleLabel.frame)+15, HHBWIDTH - 30, 60);

    [self.bgView addSubview:onlyPriceBtn];

    

    UILabel *btnTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 60)];

    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 = [[NSMutableAttributedString alloc]initWithString:btnTitleLabel.text];

    

    [attributer addAttribute:NSForegroundColorAttributeName

                       value:kHuColor(#F0493D)

                       range:NSMakeRange(0, 8)];

    [attributer addAttribute:NSFontAttributeName

                       value:[UIFont customFontSize:16]

                       range:NSMakeRange(0, 8)];

 

    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];

 

效果图

技术分享

 

 

 

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