设置navigationItem标题
// 设置导航条标题
- (void)setupNavBar {
// attributedText属性就可以设置富文本
NSString *name = @"江边一条鱼";
// NSString *name = nil;
if (name) {
// 构建文字
NSString *prefix = @"开始拖拽";
NSString *text = [NSString stringWithFormat:@"%@\n%@", prefix, name];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:text];
[string addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:15] range:[text rangeOfString:prefix]];
[string addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:[text rangeOfString:name]];
// 创建label
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.attributedText = string;
titleLabel.numberOfLines = 0;
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.width = 100;
titleLabel.height = 44;
self.navigationItem.titleView = titleLabel;
} else {
self.title = @"开始拖拽";
}
}
使用attributedText的属性进行富文本绘制。