-1

How to set the different color for numbers and words present in NSString dynamically.

I need this without using other classes. Is there any easy way to do it with NSAttributedString. I need this for UILabel.

Eg: 1 ball, 1 bat, 3 stumps, 4 Gloves,... n. etc*

I want the counts in one color and the item names in other color. Any help is appreciated.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
arunit21
  • 670
  • 1
  • 11
  • 25

2 Answers2

2

You can use NSRegularExpression to find numbers in your text and after that just add attribute to the attribute string:

    NSString *testString = @"1 ball, 1 bat, 3 stumps, 4 Gloves";
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:testString];

    NSRange   searchedRange = NSMakeRange(0, [testString length]);
    NSString *pattern = @"\\d+";
    NSError  *error = nil;

    NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];

    NSArray* matches = [regex matchesInString:testString options:0 range: searchedRange];
    for (NSTextCheckingResult* match in matches)
    {
        NSString* matchText = [testString substringWithRange:[match range]];
        [attStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:16] range:[match range]];
        NSLog(@"Match: %@", matchText);
    }
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(10, 300, 300, 30)];
    lab.attributedText = attStr;
    [self.view addSubview:lab];
Greg
  • 25,317
  • 6
  • 53
  • 62
1

Using NSAttributedString is actually using different class :) However I advise you to prepare it with NSMutableAttributedString and then store non-mutable version as it is easy to read. Anyway some untested code should look like:

NSMutableAttributedString* message = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"1 word 2 word"] attributes:nil];
[message addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, 1)];
[message addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(8, 1)];

Also you can directly append NSAttributedString ' s with desired properties instead of setting them with range:

    NSMutableAttributedString* message = [[NSMutableAttributedString alloc] init];
//set text
[message appendAttributedString:[[NSAttributedString alloc] initWithString:@"1" attributes:@{
                                                                                             NSFontAttributeName : [UIColor greenColor]
                                                                                             }]];
[message appendAttributedString:[[NSAttributedString alloc] initWithString:@" word"]];

[message appendAttributedString:[[NSAttributedString alloc] initWithString:@"2" attributes:@{
                                                                                             NSFontAttributeName : [UIColor redColor]
                                                                                             }]];
[message appendAttributedString:[[NSAttributedString alloc] initWithString:@" word"]];
hris.to
  • 6,235
  • 3
  • 46
  • 55