????6.??????????????????濪????????????????????????????????????????????У????????????е?λ????????????У?????????????????С????????£?
1     //??????????????д洢???????????????λ??
2     NSMutableArray *imageArray = [NSMutableArray arrayWithCapacity:resultArray.count];
3
4     //???????Χ????????????????滻
5     for(NSTextCheckingResult *match in resultArray) {
6         //???????????е??range
7         NSRange range = [match range];
8
9         //??????????ж?????
10         NSString *subStr = [str substringWithRange:range];
11
12         for (int i = 0; i < face.count; i ++)
13         {
14             if ([face[i][@"chs"] isEqualToString:subStr])
15             {
16
17                 //face[i][@"gif"]??????????????
18                 //??????????????????????
19                 NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
20
21                 //???????????
22                 textAttachment.image = [UIImage imageNamed:face[i][@"png"]];
23
24                 //????????????????????????滻?????????е????????
25                 NSAttributedString *imageStr = [NSAttributedString attributedStringWithAttachment:textAttachment];
26
27                 //?????????????λ??????????
28                 NSMutableDictionary *imageDic = [NSMutableDictionary dictionaryWithCapacity:2];
29                 [imageDic setObject:imageStr forKey:@"image"];
30                 [imageDic setObject:[NSValue valueWithRange:range] forKey:@"range"];
31
32                 //??????????????
33                 [imageArray addObject:imageDic];
34
35             }
36         }
37     }
????7.????????????????attributeString?????滻???滻?????????????滻????????????滻???????range????????λ?ò??????????滻???????£?
1     //???????滻
2     for (int i = imageArray.count -1; i >= 0; i--)
3     {
4         NSRange range;
5         [imageArray[i][@"range"] getValue:&range];
6         //?????滻
7         [attributeString replaceCharactersInRange:range withAttributedString:imageArray[i][@"image"]];
8
9     }
????8.???滻??????????????????TextView
????1     //???滻?????????????TextView
????2     self.myTextView.attributedText = attributeString;
????9.?滻???Ч?????£?