iOS中的图文混排

废话不多说,直接上图:

图文混排实现代码:

// 初始化可插入图片的字符串
        NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] init];
        // 拼接之前的字符(否则会覆盖掉前面输入的字符)
        [attributeStr appendAttributedString:self.attributedText];
        
        // 加载图片,EmotionAttachment是我自定义的类,继承自NSTextAttachment.
        EmotionAttachment *attach = [[EmotionAttachment alloc] init];
        attach.emotion = emotion;
        
        CGFloat imageHY = self.font.lineHeight;
        // 设置图片居中
        attach.bounds = CGRectMake(0, -4, imageHY, imageHY);
        NSAttributedString *imageStr = [NSAttributedString attributedStringWithAttachment:attach];
        // 获取光标位置
        NSUInteger loc = self.selectedRange.location;
        //[attributeStr appendAttributedString:imageStr];
        // 插入图片到光标所在位置
        [attributeStr insertAttributedString:imageStr atIndex:loc];
        
        [attributeStr addAttribute:NSFontAttributeName value:self.font range:NSMakeRange(0,attributeStr.length)];
        self.attributedText = attributeStr;
        // 移动光标到插入的图片后面
        self.selectedRange = NSMakeRange(loc + 1, 0);

 

 

 

转载于:https://www.cnblogs.com/zwois/p/4635177.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值