iOS使用NSMutableAttributedString 实现富文本

 /** 文字显示 */
    paragraphStr = [[NSMutableAttributedString alloc] init];
    NSMutableAttributedString *strName0 = [[NSMutableAttributedString alloc]initWithString:@"去"];
    [strName0 addAttribute:NSForegroundColorAttributeName value:[SQColorDefine color:SQColorGray010] range:NSMakeRange(0, 1)];
    [strName0 addAttribute:NSFontAttributeName value:[SQFontDefine font:SQFontBoldSize16] range:NSMakeRange(0, 1)];
    
    NSMutableAttributedString *strName1 = [[NSMutableAttributedString alloc]initWithString:@"偷"];
    [strName1 addAttribute:NSForegroundColorAttributeName value:[SQColorDefine color:SQColorRed008] range:NSMakeRange(0, 1)];
    [strName1 addAttribute:NSFontAttributeName value:[SQFontDefine font:SQFontBoldSize19] range:NSMakeRange(0, 1)];
    
    NSMutableAttributedString *strName2 = [[NSMutableAttributedString alloc]initWithString:@"邻居的卡"];
    [strName2 addAttribute:NSForegroundColorAttributeName value:[SQColorDefine color:SQColorGray010] range:NSMakeRange(0, 4)];
    [strName2 addAttribute:NSFontAttributeName value:[SQFontDefine font:SQFontBoldSize16] range:NSMakeRange(0, 4)];
    
    [paragraphStr appendAttributedString:strName0];
    [paragraphStr appendAttributedString:strName1];
    [paragraphStr appendAttributedString:strName2];
    
    titileLabel = [[UILabel alloc]init];
    [titileLabel setFont:[SQFontDefine font:SQFontPingFangSize19]];
    [titileLabel setTextColor:[SQColorDefine color:SQColorGray010]];
    [titileLabel setAttributedText:paragraphStr];
    [yellowBackGroundView addSubview:titileLabel];

最终结果:


28325367-22B7-4100-87AC-075A953FA4FB.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容