首页 > 其他 > 详细

属性字符串添加图片

时间:2018-05-16 10:14:06      阅读:221      评论:0      收藏:0      [点我收藏+]

- (NSMutableAttributedString *)getAttributedStringWithSting:(NSString *)astring image:(UIImage *)image index:(NSInteger)index{
        // 1.创建一个富文本
    NSMutableAttributedString *attri =  [[NSMutableAttributedString alloc] initWithString:astring];
    
//        // 修改富文本中的不同文字的样式
//    [attri addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 5)];
//    [attri addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, 5)];
//
//        // 设置数字为红色
//    [attri addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5, 9)];
//    [attri addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30] range:NSMakeRange(5, 9)];
    
        // 2.添加表情图片
    NSTextAttachment *attch = [[NSTextAttachment alloc] init];
        // 表情图片
    attch.image = image;
        // 设置图片大小
    attch.bounds = CGRectMake(0, -3, 16, 16);
    
        // 创建带有图片的富文本
    NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];
    [attri insertAttributedString:string atIndex:index];// 插入某个位置
    
    return attri;
}

属性字符串添加图片

原文:https://www.cnblogs.com/chaicla-ios/p/9044436.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!