UILabel Text with Multiple Font Colors

前端 未结 8 666
一向
一向 2020-12-19 18:48

Is there a way to have the textColor property of a UILabel be two different UIColors? Basically I\'m trying to make the first characte

8条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-19 19:30

    Starting from ios 6 you can do the following :

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]  initWithString:string];
    [attributedString addAttribute:NSBackgroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,3)];
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(4,9)];
    cell.label.attributedText = attributedString;
    

提交回复
热议问题