iOS UILabel換行同時修改字體大小顏色


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

 

效果圖

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM