Any way to bold part of a NSString?

Zhen picture Zhen · May 16, 2011 · Viewed 83k times · Source

Is there any way to bold only part of a string? For example:

Approximate Distance: 120m away

Thanks!

Answer

Jacob Relkin picture Jacob Relkin · May 16, 2011

What you could do is use an NSAttributedString.

NSString *boldFontName = [[UIFont boldSystemFontOfSize:12] fontName];
NSString *yourString = ...;
NSRange boldedRange = NSMakeRange(22, 4);

NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:yourString];

[attrString beginEditing];
[attrString addAttribute:kCTFontAttributeName 
                   value:boldFontName
                   range:boldedRange];

[attrString endEditing];
//draw attrString here...

Take a look at this handy dandy guide to drawing NSAttributedString objects with Core Text.