IOS¿ª·¢Ö®ÏÔʾ΢²©±íÇé

·¢±íÓÚ£º2014-9-15 10:03

×ÖÌ壺´ó ÖРС | ÉÏһƪ | ÏÂһƪ | ÎÒҪͶ¸å

 ×÷ÕߣºÇàÓñ·ü°¸    À´Ô´£º51TestingÈí¼þ²âÊÔÍø²É±à

·ÖÏí£º
¡¡¡¡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.Ì滻ǰºóЧ¹ûÈçÏ£º
22/2<12
¼ÛÖµ398ÔªµÄ²âÊԿγÌÃâ·ÑÔùËÍ£¬ÌîÎʾíÁìÈ¡°É£¡

¹Ø×¢51Testing

ÁªÏµÎÒÃÇ

¿ì½ÝÃæ°å Õ¾µãµØͼ ÁªÏµÎÒÃÇ ¹ã¸æ·þÎñ ¹ØÓÚÎÒÃÇ Õ¾³¤Í³¼Æ

·¨ÂɹËÎÊ£ºÉϺ£äîϪÂÉʦÊÂÎñËù ÏîÆåÂÉʦ
°æȨËùÓÐ ÉϺ£²©Îª·åÈí¼þ¼¼Êõ¹É·ÝÓÐÏÞ¹«Ë¾ Copyright©51testing.com 2003-2022
ͶËß¼°Òâ¼û·´À¡£ºwebmaster@51testing.com; ÒµÎñÁªÏµ£ºservice@51testing.com 021-64471599-8017

»¦ICP±¸05003035ºÅ

»¦¹«Íø°²±¸ 31010102002173ºÅ