Home > Blockchain >  Attributed string color- Objective-c
Attributed string color- Objective-c

Time:04-29

I have a display like in the picture enter image description here

Im getting this from a string str which is like below enter image description here

Code I have tried

lbl1.text=newStr;
NSString *textxtra = @"Xtra";
NSString *textremove = @"Remove";

NSMutableAttributedString *attrsString = [[NSMutableAttributedString alloc] initWithAttributedString:lbl1.attributedText];

 // search for word occurrence
                    
NSRange range = [lbl1.text rangeOfString:textxtra];
NSRange range1 = [lbl1.text rangeOfString:textremove];
if (range.location != NSNotFound) {
    [attrsString addAttribute:NSForegroundColorAttributeName value:[UIColor systemGreenColor] range:range];
}
if (range1.location != NSNotFound) {
    [attrsString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range1];
}

// set attributed text
lbl1.attributedText = attrsString;

How to get the string before Xtra (American Cheese) and after Xtra($1) in green color ?

The part before Remove(House Mayo) in red color? i.e The whole string American Cheese: Xtra $1 should be in green color. I get the idea to take the string in between \n. i.e string before Xtra upto \n and after Xtra upto \n But couldn't understand exactly how to implement

Any ideas/suggestions will be helpful

CodePudding user response:

Not tested, but this should do the trick:

NSString *initialSting = @"";

NSArray *lines = [initialSting componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];

NSMutableArray *attributedLines = [[NSMutableArray alloc] init];
NSDictionary *redAttributes =  @{};
NSDictionary *greenAttributes =  @{};

for (NSString *aLine in lines) 
{
    NSAttributedString *aLineAttributedString;
    //Here, you could also check, that the string is like "someString: Xtra someOtherString", because if Xtra is misplaced...
    if ([aLine containsString:@"Xtra"]) 
    {
        aLineAttributedString = [[NSAttributedString alloc] initWithString:aLine attributes:greenAttributes];
        //Here, you could also check, that the string is like "someString: Remove someOtherString", because if Remove is misplaced...
    }
    else if ([aLine containsString:@"Remove"]) 
    {
        aLineAttributedString = [[NSAttributedString alloc] initWithString:aLine attributes:redAttributes];
    } 
    else 
    {
        aLineAttributedString = [[NSAttributedString alloc] initWithString:aLine];
    }
    [attributedLines addObject:aLineAttributedString];
}

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
NSAttributedString *newLine = [[NSAttributedString alloc] initWithString:@"\n"];

if ([attributedLines count] > 0) 
{
    for (NSUInteger i = 0; i < [attributedLines count] - 1; i  ) 
    {
        [attributedString appendAttributedString:attributedLines[i]];
        [attributedString appendAttributedString:newLine];
    }
    [attributedString appendAttributedString:[attributedLines lastObject]];
}

The logic:
Get an array of NSString for each line with componentsSeparatedByCharactersInSet:
Create an array of NSAttributedString, that will be populated for each line.
For each line, color it if needed, then add it to the array.
Finally, there is no componentsJoinedByString: for NSAttributedString, so do a manual for loop to reconstruct the final NSAttributedString.

  • Related