iOS中UILabel显示不同的样式的文字属性

作者: shaneZhang 分类: ios技术 发布时间: 2014-08-29 10:25

– (void)updateTextLabelWith:(NSInteger)count withTotalPrice:(CGFloat)price

 {

    NSString *countString = [NSString stringWithFormat:@”%d”, count];

    NSString *priceString = [NSString stringWithFormat:@”%.2f”, price];

    NSString *tempString = [NSString stringWithFormat:@”您已点 %@ 个菜,共计 ¥%@”, countString, priceString];

    NSInteger tempCount = 0;

    tempCount += 4;

    // 重新生成一个可变的字符串

    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:tempString];

    [str addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] 

range:NSMakeRange(0, tempCount)];

    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@”HelveticaNeue” size:18.0]

range:NSMakeRange(0, tempCount)];

    [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHex:0xfb3a42 alpha:1.0f]

 range:NSMakeRange(tempCount, countString.length)];

    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@”HelveticaNeue” size:30.0] 

range:NSMakeRange(tempCount, countString.length)];

    tempCount += countString.length + 1;

    [str addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] 

range:NSMakeRange(tempCount, 6)];

    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@”HelveticaNeue” size:18.0]

 range:NSMakeRange(tempCount, 6)];

    tempCount += 6;

    [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHex:0xfb3a42 alpha:1.0f] 

range:NSMakeRange(tempCount, 1)];

    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@”HelveticaNeue” size:30.0] 

range:NSMakeRange(tempCount, 1)];

    tempCount += 1;

    [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHex:0xfb3a42 alpha:1.0f]

 range:NSMakeRange(tempCount, priceString.length)];

    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@”HelveticaNeue” size:30.0] 

range:NSMakeRange(tempCount, priceString.length)];

    [self.textLabel setAttributedText:str];

    [str release];

}


如果觉得我的文章对您有用,请随意打赏。如果有其他问题请联系博主QQ(909491009)或者下方留言!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注