做项目的时候遇到类似微博那种在TableView的Cell里显示文字+表情混排, 网上找了些资料,做完后模拟器调试没问题,在真机上运行时。滑动列表还是有些卡。尤其是要多次调用的情况下。
文字+图片显示要适应宽度自动换行,只能把那些字一个个拆开。循环判断文字还是图片再addSubview。判断每次添加的宽度。超出再改变坐标.
首先 h文件:
#import <Foundation/Foundation.h>
@interface FaceTextShowUtil : NSObject
+(UIView *)assembleMessageAtIndex:(NSArray *)arr:(NSDictionary*)faceTextDict:(CGFloat)maxWidth;
+(void)getImageRange:(NSString*)message :(NSMutableArray*)array;
@end
#import "FaceTextShowUtil.h"
#import <QuartzCore/QuartzCore.h>
@interface FaceTextShowUtil()
@end
@implementation FaceTextShowUtil
+(UIView *)assembleMessageAtIndex:(NSArray *)arr:(NSDictionary*)faceTextDict:(CGFloat)maxWidth
{
#define KFacialSizeWidth 24
#define KFacialSizeHeight 24
UIView *returnView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, maxWidth, 100)];
returnView.backgroundColor=[UIColor clearColor];
returnView.alpha=1.0;
NSArray *data = arr;
UIFont *fon=[UIFont systemFontOfSize:13.0f];
CGFloat upX=0;
CGFloat upY=0;
if (data) {
for (int i=0;i<[data count];i++) {
NSString *str=[data objectAtIndex:i];
if ([str hasPrefix:@"["]&&[str hasSuffix:@"]"])
{
NSString *imageNameKey=[str substringWithRange:NSMakeRange(1, str.length-2)];
NSString* imageName=[faceTextDict valueForKey:imageNameKey];
if (imageName==nil) {
if (upX > maxWidth)
{
upY = upY + KFacialSizeHeight;
upX = 0;
}
if ([imageNameKey hasPrefix:@"{"]&&[imageNameKey hasSuffix:@"}"])
{
NSString *userName=[imageNameKey substringWithRange:NSMakeRange(1, imageNameKey.length-2)];
CGSize size=[userName sizeWithFont:fon constrainedToSize:CGSizeMake(maxWidth, 99999)];
UILabel *la = [[UILabel alloc] initWithFrame:CGRectMake(upX, upY,size.width,size.height)];
la.backgroundColor=[UIColor clearColor];
la.font = fon;
la.text = userName;
la.textColor=[UIColor colorWithRed:0.1667f green:0.5843f blue:0.8471 alpha:1.0f];
[returnView addSubview:la];
[la release];
upX=upX+size.width;
}
else
{
NSString *temp =[@"[" stringByAppendingFormat:@"%@]", imageNameKey];
for (int j = 0; j<[temp length]; j++)
{
NSString *tempa = [str substringWithRange:NSMakeRange(j, 1)];
if (upX > maxWidth)
{
upY = upY + KFacialSizeHeight;
upX = 0;
}
CGSize size=[tempa sizeWithFont:fon constrainedToSize:CGSizeMake(maxWidth, 99999)];
UILabel *la = [[UILabel alloc] initWithFrame:CGRectMake(upX, upY,size.width,size.height)];
la.backgroundColor=[UIColor clearColor];
la.font = fon;
la.text = tempa;
[returnView addSubview:la];
[la release];
upX=upX+size.width;
}
}
}
else
{
if (upX+KFacialSizeWidth > maxWidth)
{
upY = upY + KFacialSizeHeight;
upX = 0;
}
UIImageView *img=[[UIImageView alloc]initWithImage:[UIImage imageNamed:imageName]];
img.frame = CGRectMake(upX, upY-3, KFacialSizeWidth, KFacialSizeHeight);
[returnView addSubview:img];
[img release];
upX=KFacialSizeWidth+upX;
}
}else
{
for (int j = 0; j<[str length]; j++)
{
NSString *temp = [str substringWithRange:NSMakeRange(j, 1)];
if (upX > maxWidth)
{
upY = upY + KFacialSizeHeight;
upX = 0;
}
CGSize size=[temp sizeWithFont:fon constrainedToSize:CGSizeMake(maxWidth, 99999)];
UILabel *la = [[UILabel alloc] initWithFrame:CGRectMake(upX, upY,size.width,size.height)];
la.backgroundColor=[UIColor clearColor];
la.font = fon;
la.text = temp;
[returnView addSubview:la];
[la release];
upX=upX+size.width;
}
}
}
}
if (upY==0) {
[returnView setFrame:CGRectMake(10, 10, upX, upY+13)];
}
else {
[returnView setFrame:CGRectMake(10, 10, maxWidth, upY+13)];
}
if ([data count]==1&&[[data objectAtIndex:0]length]==0) {
[returnView setFrame:CGRectMake(10, 10, upX, upY)];
}
return returnView;
}
//解析输入的文本,根据文本信息分析出那些是表情,那些是文字。
+(void)getImageRange:(NSString*)message :(NSMutableArray*)array
{
NSRange range=[message rangeOfString:@"["];
NSRange range1=[message rangeOfString:@"]"];
//判断当前字符串是否还有表情的标志。
if (range.length&&range1.length) {
if (range.location>0) {
if (range.location>range1.location) {
[array addObject:[message substringWithRange:NSMakeRange(0, range.location)]];
NSString *str=[message substringFromIndex:range.location];
[self getImageRange:str :array];
}
else
{
[array addObject:[message substringToIndex:range.location]];
[array addObject:[message substringWithRange:NSMakeRange(range.location, range1.location+1-range.location)]];
NSString *str=[message substringFromIndex:range1.location+1];
[self getImageRange:str :array];
}
}else {
NSString *nextstr=[message substringWithRange:NSMakeRange(range.location, range1.location+1-range.location)];
//排除文字是“”的
if (![nextstr isEqualToString:@""]) {
[array addObject:nextstr];
NSString *str=[message substringFromIndex:range1.location+1];
[self getImageRange:str :array];
}else {
return;
}
}
}else {
[array addObject:message];
}
}
@end
在填充Cell里:
NSArray*subviews=[[NSArray alloc]initWithArray:cell.viewTargetContent.subviews];
for (UIView*subview in subviews) {
[subview removeFromSuperview];
}
[subviews release];
UIView*viewContent=nil;
NSMutableArray * array =[[NSMutableArray alloc] init];
[FaceTextShowUtil getImageRange:content :arrayShare];
viewContent=[FaceTextShowUtil assembleMessageAtIndex:array:context.faceTextDict:225];
[cell.viewTargetContent addSubview: viewContent];
这里修复了网上那流传的DEMO中 [不是表情]什么都不显示的bug, 还有打”]][[[“这种内容会崩溃的bug。
还有 “[{这里会变色}]”加上这种符号后,文字换个颜色。以供用户需求。