//
//  NSMutableAttributedString+Attachment.m
//  Gengmei
//
//  Created by wangyang on 2018/8/7.
//  Copyright © 2018年 更美互动信息科技有限公司. All rights reserved.
//

#import "NSMutableAttributedString+Attachment.h"
#import "NSString+GM.h"
@implementation NSMutableAttributedString (Attachment)
- (void)addFeedIsTopAttachment {
    NSTextAttachment* textAttachment = [[NSTextAttachment alloc] init];
    textAttachment.image = [UIImage imageNamed:@"feed_is_top"];
    textAttachment.bounds = CGRectMake(0, -1, 13, 13);  // 微调图片位置

    NSAttributedString *imageAttachment = [NSAttributedString attributedStringWithAttachment:textAttachment];
    // 给最后一个字符尾部添加字符水平间距，以符合设计要求：箭头与文字的间距为8个点
    [self addAttribute:NSKernAttributeName value:@(8) range:NSMakeRange(self.string.length - 1, 1)];
    [self insertAttributedString:imageAttachment atIndex:self.string.length];
}

- (void)addDiaryMatchAttachment {
    NSTextAttachment* textAttachment = [[NSTextAttachment alloc] init];
    textAttachment.image = [UIImage imageNamed:@"diary_match"];//diary_match//feed_is_top
    textAttachment.bounds = CGRectMake(0, -2, 31, 16);  // 微调图片位置

    NSAttributedString *imageAttachment = [NSAttributedString attributedStringWithAttachment:textAttachment];
    // 给最后一个字符尾部添加字符水平间距，以符合设计要求：箭头与文字的间距为8个点
    [self addAttribute:NSKernAttributeName value:@(8) range:NSMakeRange(self.string.length - 1, 1)];
    [self insertAttributedString:imageAttachment atIndex:self.string.length];
}

- (NSAttributedString *)addGroupBuyIsFrontAttachment:(NSString *)iconName {
    [self addGroupBuyIsFrontAttachment:iconName attachmentBounds:CGRectMake(0, -1, 25, 13)];
//    [self addAttribute:NSKernAttributeName value:@(20) range:NSMakeRange(0, 1)];
    return self;
}

- (NSAttributedString *)addGroupBuyIsFrontAttachment:(NSString *)iconName attachmentBounds:(CGRect)attachmentBounds {
    NSMutableAttributedString *textAttrStr = [[NSMutableAttributedString alloc] init];
    NSTextAttachment* textAttachment = [[NSTextAttachment alloc] init];
    textAttachment.image = [UIImage imageNamed:iconName.isNonEmpty ? iconName : @"groupBuy_icon"];
    textAttachment.bounds = CGRectMake(attachmentBounds.origin.x, attachmentBounds.origin.y, attachmentBounds.size.width, attachmentBounds.size.height);  // 微调图片位置
    
    NSAttributedString *imageAttachment = [NSAttributedString attributedStringWithAttachment:textAttachment];
    [textAttrStr appendAttributedString:imageAttachment];
    //标签后添加空格
    [textAttrStr appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
    // 给最后一个字符尾部添加字符水平间距，以符合设计要求：箭头与文字的间距为8个点
    [self insertAttributedString:textAttrStr atIndex:0];
    return self;
}

@end
