ios 在文字里加图片

interface NSMutableAttributedString(CustomImage)

/**
在尾部添加图片附件

@param image 图片
@param point x水平偏移,但是排版位置不变,向后移动,会被截断,所以推荐使用空格实现水平偏移。
y为垂直偏移,正为向上,负为向下
*/
- (void)appendImageAttachment:(UIImage *)image offset:(CGPoint)point;

@end

@implementation NSMutableAttributedString(CustomImage)

- (void)appendImageAttachment:(UIImage *)image offset:(CGPoint)point;
{
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = image;
attachment.bounds = CGRectMake(point.x, point.y, attachment.image.size.width, attachment.image.size.height);

NSAttributedString *attach = [NSAttributedString attributedStringWithAttachment:attachment];
[self appendAttributedString:attach];
}

@end

发表评论

电子邮件地址不会被公开。