iOS-图文混排(NSMutableAttributedString)

- (NSMutableAttributedString *)AttributedString:(NSString *)name content:(NSString *)content
{
    // 富文本技术:
    // 1.图文混排
    // 2.随意修改文字样式

    //拿到整体的字符串
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@",name,content]];
    
    // 设置 name 颜色,也可以设置字体大小等
    [string addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#576b95"] range:NSMakeRange(0, name.length)];
    

    // 创建图片图片附件
    NSTextAttachment *attach = [[NSTextAttachment alloc] init];
    attach.image = [UIImage imageNamed:@"detail_icon_caption_black_normal_14x14_"];
    attach.bounds = CGRectMake(0, 0, 14, 14);
    NSAttributedString *attachString = [NSAttributedString attributedStringWithAttachment:attach];
   
    //将图片插入到合适的位置
    [string insertAttributedString:attachString atIndex:0];
    
    return string;
    
}

D4E644B4-E34B-4D74-8AE6-7D9AE5431A64.png

你可能感兴趣的:(iOS-图文混排(NSMutableAttributedString))