美文网首页
ios 图文混排 (支持显示文字和表情)

ios 图文混排 (支持显示文字和表情)

作者: 零零贰叁 | 来源:发表于2016-10-13 10:35 被阅读163次

#import "KitchenShowFaces.h"

#define BEGIN_FLAG @"{"

#define END_FLAG @"}"

@implementationKitchenShowFaces

-(void)getImageRange:(NSString*)message : (NSMutableArray*)array {

NSRange range=[messagerangeOfString:BEGIN_FLAG];

NSRange range1=[messagerangeOfString:END_FLAG];

//判断当前字符串是否还有表情的标志。

if(range.length&& range1.length) {

if(range.location>0) {

[arrayaddObject:[messagesubstringToIndex:range.location]];

[arrayaddObject:[messagesubstringWithRange:NSMakeRange(range.location, range1.location+1-range.location)]];

NSString*str=[messagesubstringFromIndex:range1.location+1];

[selfgetImageRange:str :array];

}else{

NSString*nextstr=[messagesubstringWithRange:NSMakeRange(range.location, range1.location+1-range.location)];

//排除文字是“”的

if(![nextstrisEqualToString:@""]) {

[arrayaddObject:nextstr];

NSString*str=[messagesubstringFromIndex:range1.location+1];

[selfgetImageRange:str :array];

}else{

return;

}

}

}else{

if(message !=nil) {

[arrayaddObject:message];

}

}

}

#define KFacialSizeWidth  24

#define KFacialSizeHeight 24

#define MAX_WIDTH 240

-(UIView*)assembleMessageAtIndex : (NSString*) message

{

NSMutableArray*array = [[NSMutableArrayalloc]init];

[selfgetImageRange:message :array];

UIView*returnView = [[UIViewalloc]initWithFrame:CGRectZero];

NSArray*data = array;

UIFont*fon = [UIFontsystemFontOfSize:14.0f];

CGFloat upX =0;

CGFloat upY =0;

if(data) {

for(inti=0;i<[datacount];i++) {

NSString*str=[dataobjectAtIndex:i];

if([strhasPrefix:BEGIN_FLAG]&&[strhasSuffix:END_FLAG])

{

if(upX > MAX_WIDTH)

{

upY = upY + KFacialSizeHeight;

upX =0;

}

NSString*imageName=[strsubstringWithRange:NSMakeRange(1, str.length-2)];

UIImageView*img=[[UIImageViewalloc]initWithImage:[UIImageimageNamed:imageName]];

img.frame= CGRectMake(upX, upY, KFacialSizeWidth, KFacialSizeHeight);

[returnViewaddSubview:img];

upX=KFacialSizeWidth+upX;

}else{

for(intj =0; j < [strlength]; j++) {

NSString*temp = [strsubstringWithRange:NSMakeRange(j,1)];

if(upX > MAX_WIDTH)

{

upY = upY + KFacialSizeHeight;

upX =0;

}

CGSize size=[tempsizeWithFont:fonconstrainedToSize:CGSizeMake(320,40)];

UILabel*la = [[UILabelalloc]initWithFrame:CGRectMake(upX,upY,size.width,size.height)];

la.font= fon;

la.text= temp;

[returnViewaddSubview:la];

upX=upX+size.width;

}

}

}

}

returnView.frame= CGRectMake(0,0, upX, upY);//@ 需要将该view的尺寸记下,方便以后使用

returnreturnView;

}

@end

相关文章

网友评论

      本文标题:ios 图文混排 (支持显示文字和表情)

      本文链接:https://www.haomeiwen.com/subject/kjjwyttx.html