2

I have a search any specific word like "God". I want to change that specific word text color should be change in different color. I have try to search on google but I have found out that a specific word color change for particular index value from start to end of word Link that's why my problem not solve.

Please Help me and update my code for change the specific word text color i.e the word in the box you have see in the image.

Thanks

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:UYLCellIdentifier forIndexPath:indexPath];
    [self configureCell:cell forRowAtIndexPath:indexPath];
     return cell;
}

- (void)configureCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell isKindOfClass:[UYLTextCellSearch class]])
    {
         UYLTextCellSearch *textCell = (UYLTextCellSearch *)cell;

         DataBase *DBContentDetail = [_sourceData objectAtIndex:indexPath.row];

         textCell.lineLabel.text = [NSString stringWithFormat:@"%@",DBContentDetail.dbtext];
        [textCell.lineLabel sizeToFit];
    }
}

How to change the color of a specific word in ios

Rahul Saini
  • 921
  • 2
  • 10
  • 23
  • possible duplicate of [Is it possible to change color of single word in UITextView and UITextField](http://stackoverflow.com/questions/14231879/is-it-possible-to-change-color-of-single-word-in-uitextview-and-uitextfield) – Maciej Kozieł Sep 18 '14 at 11:05

2 Answers2

10
NSString *text = @"In the begining God created heaven and earth. God is great.";

NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithString:text];

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(God)" options:kNilOptions error:nil]; // Matches 'God' case SENSITIVE

NSRange range = NSMakeRange(0 ,text.length);

// Change all words that are equal to 'God' to red color in the attributed string
[regex enumerateMatchesInString:text options:kNilOptions range:range usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {

    NSRange subStringRange = [result rangeAtIndex:0];
    [mutableAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:subStringRange];
}];

textCell.lineLabel.attributedText = mutableAttributedString;

More on NSAttributedString

Shai
  • 25,159
  • 9
  • 44
  • 67
0

Yes, you can change the color of a specific word Using "Text Kit framework" introduced in iOS 7.

look out for the statement "In the model-view-controller paradigm, the layout manager is the controller. NSTextStorage, a subclass of NSMutableAttributedString, provides part of the model, holding a string of text characters with attributes such as typeface, style, color" documented here: (https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/CustomTextProcessing/CustomTextProcessing.html)

or else you can have a look at example given in this video (https://developer.apple.com/videos/wwdc/2013/#210) of WWDC 2013.

Pankaj Yadav
  • 601
  • 9
  • 23