自己电脑做网站需要什么设备,哈尔滨网站建设吕新松,重庆建设教育培训管理系统,做软件推广网站怎么赚钱苹果在iOS7中推出了一个新的类NSTextAttachment#xff0c;它是做图文混排的利器#xff0c;本文就是用这个类#xff0c;只用50行代码实现文字与表情混排#xff0c;当然也可以实现段落中的图文混排。 首先说一下文字和表情的混排#xff1a; 先来做点儿准备工作#… 苹果在iOS7中推出了一个新的类NSTextAttachment它是做图文混排的利器本文就是用这个类只用50行代码实现文字与表情混排当然也可以实现段落中的图文混排。 首先说一下文字和表情的混排 先来做点儿准备工作搞一个存放表情信息的plist文件 NSString *filePath [[NSBundle mainBundle] pathForResource:Image ofType:plist];NSArray *face [NSArray arrayWithContentsOfFile:filePath];NSMutableAttributedString *attributeString [[NSMutableAttributedString alloc] initWithString:text];NSString *regex_emoji \\[[a-zA-Z0-9\\/\\u4e00-\\u9fa5]\\];//匹配表情NSError *error nil;NSRegularExpression *re [NSRegularExpression regularExpressionWithPattern:regex_emoji options:NSRegularExpressionCaseInsensitive error:error];if(!re) {NSLog(%, [error localizedDescription]);return attributeString;}NSArray *resultArray [re matchesInString:text options:0 range:NSMakeRange(0, text.length)];NSMutableArray *imageArray [NSMutableArray arrayWithCapacity:resultArray.count];//根据匹配范围来用图片进行相应的替换for(NSTextCheckingResult *match in resultArray) {//获取数组元素中得到rangeNSRange range [match range];//获取原字符串中对应的值NSString *subStr [text substringWithRange:range];for(int i 0; i face.count; i ) {if([face[i][cht] isEqualToString:subStr]) {//face[i][png]就是我们要加载的图片//新建文字附件来存放我们的图片,iOS7才新加的对象NSTextAttachment *textAttachment [[NSTextAttachment alloc]init];//给附件添加图片textAttachment.image [UIImage imageNamed:face[i][png]];//调整一下图片的位置,如果你的图片偏上或者偏下调整一下bounds的y值即可textAttachment.boundsCGRectMake(0, -8, textAttachment.image.size.width, textAttachment.image.size.height);//把附件转换成可变字符串用于替换掉源字符串中的表情文字NSAttributedString *imageStr [NSAttributedString attributedStringWithAttachment:textAttachment];//把图片和图片对应的位置存入字典中NSMutableDictionary *imageDic [NSMutableDictionary dictionaryWithCapacity:2];[imageDic setObject:imageStr forKey:image];[imageDic setObject:[NSValue valueWithRange:range]forKey:range];//把字典存入数组中[imageArray addObject:imageDic];}}}//4、从后往前替换否则会引起位置问题for(int i (int)imageArray.count-1; i 0; i--) {NSRange range;[imageArray[i][range] getValue:range];//进行替换[attributeString replaceCharactersInRange:range withAttributedString:imageArray[i][image]];}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485861234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 以上代码段是写了一个工具类以实现图文混排也可以为NSString写一个分类来实现。下面说一下用法
NSString *content 文字加上表情[得意][酷][呲牙];NSMutableAttributedString *attrStr [Utility emotionStrWithString:content];_contentLabel.attributedText attrStr;
123456123456 有了上面的方法要实现图片和文字的排布就更容易了只需要将某些图片给它设置一个固定的字符对应即可。 具体方法如下 NSString *praiseStr ”路人甲、路人乙”; NSString *praiseInfo [NSString stringWithFormat:”点赞 %”,praiseStr]; NSDictionary *attributesForAll {NSFontAttributeName:[UIFont systemFontOfSize:14.0],NSForegroundColorAttributeName:[UIColorgrayColor]}; NSMutableAttributedString*attrStr [Utility exchangeString:”点赞”withText:praiseInfoimageName:”dynamic_love_blue”]; 有什么不足或者bug欢迎及时沟通