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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容