如何修改字符串字体,颜色,以及换行

     UILabel *titleView = [[UILabel alloc] init];
        titleView.width = 200;
        titleView.height = 44;
        titleView.textAlignment = NSTextAlignmentCenter;
        // 自动换行
        titleView.numberOfLines = 0;

        NSString *name = [HWAccountTool account].name;
        NSString *prefix = @"发微博";
    
        
        NSString *str = [NSString stringWithFormat:@"%@
%@", prefix, name];    

// 创建一个带有属性的字符串(比如颜色属性、字体属性等文字属性)
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:str];
        // 添加属性
        [attrStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:16] range:[str rangeOfString:prefix]];
        [attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:[str rangeOfString:name]];
        titleView.attributedText = attrStr;
        self.navigationItem.titleView = titleView;

效果:

原文地址:https://www.cnblogs.com/521it/p/5001612.html