ryu1
2/3/2016 - 6:11 AM

NSMutableAttributedString+Extension.h

//
//  NSMutableAttributedString+Extension.m
//
//  Created by Ryuichi Ishitsuka on 2016/01/08.
//
#import "NSMutableAttributedString+Extension.h"

@implementation NSMutableAttributedString (Extension)

-(void)insertIconImage:(UIImage*)icon bounds:(CGRect)bounds
{
    NSTextAttachment *at = [[NSTextAttachment alloc] init];
    at.image = icon;
    at.bounds = bounds;
    NSAttributedString *ns = [NSAttributedString attributedStringWithAttachment:at];
    
    // アノテーションが残ってしまうから、文字列中から削除
    NSRange range = [self.string rangeOfString:@"アノテーション"];
    [self replaceCharactersInRange:range withString:@""];
    
    // 同じところに、iconを追加
    [self insertAttributedString:ns atIndex:range.location];
    
}


@end
//
//  NSMutableAttributedString+Extension.h
//
//  Created by Ryuichi Ishitsuka on 2016/01/08.
//
#import <Foundation/Foundation.h>


@interface NSMutableAttributedString (Extension)

/**
 *  imageを文字列に挿入します。
 *
 *  @param icon    挿入するアイコン画像
 *  @param bounds   挿入枠内でのimageの座標
 */
-(void)insertIconImage:(UIImage*)icon bounds:(CGRect)bounds;

@end