2

I have multiple UILabels within a Custom Table cell. These labels contain varied text or varied length.

As it stands i have UILabel Subclassed allowing me to implement these methods

- (void)boldRange:(NSRange)range {
if (![self respondsToSelector:@selector(setAttributedText:)]) {
    return;
}
NSMutableAttributedString *attributedText;
if (!self.attributedText) {
    attributedText = [[NSMutableAttributedString alloc] initWithString:self.text];
} else {
    attributedText = [[NSMutableAttributedString alloc]initWithAttributedString:self.attributedText];
}
     [attributedText setAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:self.font.pointSize]} range:range];
self.attributedText = attributedText;
NSLog(@"%@", NSStringFromRange(range));
}

- (void)boldSubstring:(NSString*)substring {
    NSRange range = [self.text rangeOfString:substring];
    [self boldRange:range];
}

This allows me to call [cell.StoryLabel boldSubstring:@"test"]; which will BOLD the first occurrence of the word 'test'.

What i am after is the ability to either create new subclass methods or extend the ones i already have, to allow me to replace ALL occurrences of a specified word within the label.

I have looked into a number of methods including 3rd party frameworks. The trouble i have is this is a learning process for me. I would be far more beneficial for me to try and complete this myself.

Thanks in advance!

scottevans93
  • 1,119
  • 1
  • 9
  • 25
  • `rangeOfString` returns the first occurrence, that's normal behavior. You could use a `NSRegularExpression`, and use `matchesInString:options:range` to get a `NSArray` of `NSRange`, an use a for loop to bold it. – Larme May 06 '14 at 13:33
  • How would i set the `[attributedText setAttributes:` for multiple though? – scottevans93 May 06 '14 at 13:34
  • http://www.ioscreator.com/tutorials/format-text-in-ios6-attributed-strings – Macrosoft-Dev May 06 '14 at 13:38
  • http://stackoverflow.com/questions/18365631/example-of-nsattributedstring-with-two-different-font-sizes – Macrosoft-Dev May 06 '14 at 13:39
  • @Macrosoft-Dev : The issue is not about how bold (add "effects to `NSAttributedString`), but how to find subString... – Larme May 06 '14 at 14:21

1 Answers1

4

rangeOfString returns the first occurrence, that's normal behavior. From the Doc:

Finds and returns the range of the first occurrence of a given string within the receiver.

You could use a NSRegularExpression, and use matchesInString:options:range to get a NSArray of NSTextCheckingResult (that have a NSRange property), an use a for loop to bold it.

This should do the trick:

- (void)boldSubstring:(NSString*)substring
{
    if (![self respondsToSelector:@selector(setAttributedText:)])
    {
        return;
    }

    NSError *error;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern: substring options:NSRegularExpressionCaseInsensitive error:&error];

    if (!error)
    {
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:[self text]];
        NSArray *allMatches = [regex matchesInString:[self text] options:0 range:NSMakeRange(0, [[self text] length])];
        for (NSTextCheckingResult *aMatch in allMatches)
        {
            NSRange matchRange = [aMatch range];
            [attributedString setAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:self.font.pointSize]} range: matchRange];
        }
        [self setAttributedText:attributedString];
    }
}
Larme
  • 24,190
  • 6
  • 51
  • 81
  • I implemented you method but it doesn't seems to be working. The text that i am trying to highlight is still the same as the rest – scottevans93 May 06 '14 at 13:55
  • I got it working. I forgot earlier a line (setting the attributedText to self), did you add it? – Larme May 06 '14 at 13:59
  • Im not sure, which line are you talking about? – scottevans93 May 06 '14 at 14:12
  • I added:`[self setAttributedText:attributedString];`. Check in debug if `error` is not null. Check also if `[allMatches count]` is different than 0 in debugger. – Larme May 06 '14 at 14:14