¡¡¡¡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.Ì滻ǰºóЧ¹ûÈçÏ£º