Is there any way to use attributed label on navigation bar on iOS 5? I know it works in iOS 6+ but before that?
I want to use both bold and non bold font there.
Asked
Active
Viewed 5,735 times
2 Answers
4
You can use TTAttributedLabel to achieve this. Here is the code to do the trick. Suppose you want to set title to "Sample NavBarTitle" where the Sample should bold.
//In your code there may be several ranges, corresponding to how many attributes you want to have
NSRange range = NSMakeRange(0, 6);
//Initialize NSMutableAttributedString
NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@"Sample NavBarTitle"];
//Set the attribute to make "Sample" bold
[string addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Georgia-Bold" size:15] range:range];
//Initialize TTAttributedLabel with rect
TTTAttributedLabel * label = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(0, 0, 20, 150)];
//Set the attributedText property of TTAttributedLabel
label.attributedText = string;
//Set navigationItem.titleView to the label view we've created
self.navigationItem.titleView = label;
That's all.

kyurkchyan
- 2,260
- 2
- 23
- 37
-
This looks good but NSFontAttributeName is ios6 only enum. What can I use instead? – Dvole Oct 17 '13 at 09:55
-
@Dvole This answer answers your question pretty well, so please, accept it. And if you are interested in `NSFontAttributeName` like thing for other iOS versions, add new question, or edit your current question to include that. – Fahri Azimov Oct 17 '13 at 10:04
-
1@FahriAzimov not really, it crashes in iOS 5. – Dvole Oct 17 '13 at 10:09
-
Try using kCTFontAttributeName, if it doesn't work either, I'll try to provide another solution. – kyurkchyan Oct 17 '13 at 10:19
2
Here is the code that helped me:
// Total string
NSString *tempString = [NSString stringWithFormat:@"%@, %@ %@ %@ %@", gameName, stringConnector, self.bet.gameInfo.draw.drawNumber, stringConnector2, [dateFormatter stringFromDate:self.bet.gameInfo.draw.date]];
NSRange range = [tempString rangeOfString:gameName];
NSMutableAttributedString * string2 = [[NSMutableAttributedString alloc] initWithString:tempString];
// font for all string
UIFont *regularFont = [UIFont fontWithName:BEAU_PRO_LIGHT_FONT_NAME size:21];
CTFontRef font_2 = CTFontCreateWithName((__bridge CFStringRef)regularFont.fontName, regularFont.pointSize, NULL);
[string2 addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font_2 range:[tempString rangeOfString:tempString]];
// bold font
UIFont *boldFont = [UIFont fontWithName:BEAU_PRO_SEMIBOLD_FONT_NAME size:21];
CTFontRef font_1 = CTFontCreateWithName((__bridge CFStringRef)boldFont.fontName, boldFont.pointSize, NULL);
[string2 addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font_1 range:range];
TTTAttributedLabel * label = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(0, 0, 600, 20)];
label.attributedText = string2;
[label sizeToFit];
self.navigationItem.titleView = label;

Dvole
- 5,725
- 10
- 54
- 87