加入标题的一部分,iOS 7

时间:2014-03-17 05:22:40

标签: ios objective-c

我已经阅读了几种关于粗体化一部分字符串的方法。 但我仍然无法让它发挥作用。

这是我的代码

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

在viewDidLoad函数中

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;

有什么建议吗? 先感谢您。 ^^

2 个答案:

答案 0 :(得分:2)

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;

答案 1 :(得分:0)

你可以做的是使用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];

//在这里绘制attrString ... 看一下this handy dandy guide,用Core Text绘制NSAttributedString对象。