How to change colour of all numbers in a string in swift

前端 未结 6 632
灰色年华
灰色年华 2021-01-14 11:24

I am trying to change colour for all numbers present in a string in swift code.Example:

var mystring = \"abc 3423 opqrs 474598 lmno 343543\"
         


        
6条回答
  •  情话喂你
    2021-01-14 12:09

    NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:"Your Text"];
    [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,5)]; //give range where you want
    [string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(5,6)];//give range where you want
    [string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(11,5)]; //give range where you want
    YourLable.attributedText = string;
    

提交回复
热议问题