Bold a part of title, iOS 7

1k views Asked by At

I have read several method about bolding a part of string. But I still can't get it work.

Here's my code

#define FONT_OPEN_BOLD(s) [UIFont fontWithName:@"OpenSans-Bold" size:s]

In viewDidLoad function

NSString *stringName = @"ShowTimes" ;                     
UIFont *font = FONT_OPEN_BOLD(15.0f);
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:stringName];
[attrString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, 4)];

self.title = stringName;

Any suggestion? Thank you in advance. ^^

2

There are 2 answers

9
m-farhan On BEST ANSWER
NSString *stringName = @"ShowTimes" ;                     
UIFont *font = FONT_OPEN_BOLD(15.0f);
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:stringName];
[attrString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, 4)];

//Initialize TTAttributedLabel with rect
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 20, 150)];
//Set the attributedText property of TTAttributedLabel
label.attributedText = attrString;
//Set navigationItem.titleView to the label view we've created
self.navigationItem.titleView = label;
0
Macrosoft-Dev On

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.