2

Say I have NSString * hello = @"hello world";

Now I want an atributed string where the hell in hello world is bolded.

There is a function on the web that do this:

- (NSMutableAttributedString*) word:(NSString*)substringToHighlight{

    NSMutableAttributedString * mutableAttributedString = [[ NSMutableAttributedString alloc]initWithString:self];
    NSUInteger count = 0, length = [mutableAttributedString length];
    NSRange range = NSMakeRange(0, length);

    count = 0,
    length = [mutableAttributedString length];
    range = NSMakeRange(0, length);
    while(range.location != NSNotFound)
    {
        range = [[mutableAttributedString string] rangeOfString:substringToHighlight options:0 range:range];
        if(range.location != NSNotFound) {

            //[mutableAttributedString setTextColor:[UIColor blueColor] range:NSMakeRange(range.location, [word length])];
            range = NSMakeRange(range.location + range.length, length - (range.location + range.length));
            count++;
        }
    }
    return mutableAttributedString;
}

However, the function doesn't work because mutableAttributedString doestn't support setTextColor

I also tried

NSDictionary * dict = @{kCTFontAttributeName:boldFontName};
        [mutableAttributedString setAttributes:{kCTFontAttributeName:boldFontName} range:NSMakeRange(range.location, substringToHighlight.length)];

but got a message kCTFontAttributeName is not defined.

user4951
  • 32,206
  • 53
  • 172
  • 282
  • check this: http://stackoverflow.com/questions/13621245/how-to-get-nsranges-for-a-substring-in-nsstring – Nikita P Jan 30 '13 at 05:22
  • possible duplicate of [Any way to bold part of a NSString?](http://stackoverflow.com/questions/6013705/any-way-to-bold-part-of-a-nsstring) – Perception Jan 30 '13 at 05:37

1 Answers1

6

You can use rangeOfString:options:range: or NSScanner (there are other possibilities like regexps but anyway).

Finds and returns the range of the first occurrence of a given string, within the given range of the receiver, subject to given options.

  • (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask range:(NSRange)aRange

This is another solution :

Then you need to convert into NSMutableAttributedString like this way.

NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"hello world"];
NSRange selectedRange = NSMakeRange(0, 4); // 4 characters, starting at index 0

[string beginEditing];

[string addAttribute:NSFontAttributeName
           value:[NSFont fontWithName:@"Helvetica-Bold" size:12.0]
           range:selectedRange];

[string endEditing];

I think this is the best solution.

Nirav Jain
  • 5,088
  • 5
  • 40
  • 61
  • Yes. This is the good practice to use only one string and can utilize in the best way. I like it. – AppAspect Jan 30 '13 at 06:45
  • Why we need to do beginEditing and endEditing? – user4951 Jan 30 '13 at 10:31
  • NSMutableAttributedString provides/helps beginEditing and endEditing methods for subclasses of NSMutableAttributedString to override. These methods allow instances of a subclass to record or buffer groups of changes and clean themselves up on receiving an endEditing message. The endEditing method also allows the receiver to notify any observers that it has been changed. NSTextStorage’s implementation of endEditing, for example, fixes changed attributes and then notifies its layout managers that they need to re-lay and redisplay their text. The default implementations do nothing. – Nirav Jain Jan 30 '13 at 10:55