0

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

4
  • You can use rangeOfString:options:range: to search for "\n", using the options backwards, and the range to search, being 0, rangeOfXtra.location. Then, you'll can calculate the range: (rangeOfN.location, rangeOfXtra.location - rangeOfN.location). Repeat for the other one. Commented Apr 27, 2022 at 12:19
  • But a quicker solution maybe, would be to split the initial string into an array of NSString separated with "\n", create a NSMutableAttributedString empty, iterate over each string, and if the line contain one of the world, add the color effect to the whole line, and append to the attributedString. Commented Apr 27, 2022 at 12:25
  • @Larme can u post your answer. Commented Apr 27, 2022 at 12:27
  • The part before Xtra, before Remove is dynamic from server Commented Apr 27, 2022 at 12:28

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.