难点:由于有视频,语音,地理位置,文字,图片等不同的数据类型,所以相对来说布局叫复杂
主要的思路是:在获取数据的同时,计算好每个cell的布局,高度
1.数据类
@interface ShareInfoDemo : NSObject
@property(nonatomic,strong)NSString *idNum;
@property(nonatomic,strong)NSString *type;
@property(nonatomic,strong)NSString *content;
@property(nonatomic,strong)NSArray *referUris;
@property(nonatomic,strong)NSString *duration;
@property(nonatomic,strong)NSString *sendTime;
@property(nonatomic,strong)SelfDataModel *user;
@property(nonatomic,strong)NSMutableDictionary *replyPage;
@property(nonatomic,strong)NSMutableDictionary *zans;
@property(nonatomic,strong)NSString *location;
@property(nonatomic,assign)bool hasShowAll;
@end
2.计算每个cell
@interface ShareInfoFrame : NSObject
@property(nonatomic,strong)ShareInfoDemo *shareInfo;
@property(nonatomic,assign)CGRect iconImageFrame;
@property(nonatomic,assign)CGRect nameFrame;
@property(nonatomic,assign)CGRect contentFrame;
@property(nonatomic,strong)NSMutableArray * imageViewFArr;
@property(nonatomic,assign)CGRect mapFrame;
@property(nonatomic,assign)CGRect playviewFrame;
@property(nonatomic,assign)CGRect timeFrame;
@property(nonatomic,assign)CGRect blogToolBarF;
@property(nonatomic,strong)NSMutableArray *responseViewHeightArr;
@property(nonatomic,assign)CGFloat cellHeight;
@property(nonatomic,assign)CGRect zanFrame;
@property(nonatomic,assign)CGRect playSoundFrame;
@property(nonatomic,assign)CGRect delectFrame;
@property(nonatomic,assign)CGRect showAllFrame;
@end
//
// ShareInfoFrame.m
//
// Created by yinbo on 2016/11/16.
// Copyright © 2016年 yinbo. All rights reserved.
//
#import "ShareInfoFrame.h"
@implementation ShareInfoFrame
-(NSDictionary *)parseJSONStringToNSDictionary:(NSString *)JSONString {
NSData *JSONData = [JSONString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableLeaves error:nil];
return responseJSON;
}
-(void)setShareInfo:(ShareInfoDemo *)shareInfo{
_shareInfo=shareInfo;
self.imageViewFArr=[[NSMutableArray alloc]initWithCapacity:0];
self.responseViewHeightArr=[[NSMutableArray alloc]initWithCapacity:0];
//头像的Frame
CGFloat iconX = 20;
CGFloat iconY = 20;
CGFloat iconW = 40;
CGFloat iconH = 40;
self.iconImageFrame=CGRectMake(iconX, iconY, iconW, iconH);
//名字的Frame
CGFloat nameX = 70;
CGFloat nameY = 20;
CGFloat nameW = YBScreenBoundsWidth-90;
CGFloat nameH =15;
self.nameFrame = CGRectMake(nameX, nameY, nameW, nameH);
//文字内容的Frame
CGFloat contentX=70;
CGFloat contentY=40;
CGFloat contentW=YBScreenBoundsWidth-90;
CGSize size1 = CGSizeMake(YBScreenBoundsWidth-90, MAXFLOAT);
NSDictionary *dict = @{NSFontAttributeName:ContentFont};
CGSize size2 =[self.shareInfo.content boundingRectWithSize:size1 options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
CGFloat contentH = size2.height;
self.contentFrame=CGRectMake(contentX, contentY, contentW, contentH);
if (contentH>90) {
if (!self.shareInfo.hasShowAll) {
self.contentFrame=CGRectMake(contentX, contentY, contentW, 90);
}
}
//富文本内容的Frame
// bool isOver=NO;
// if (size2.height+40<60) {
// isOver=NO;
// }else{
// isOver=YES;
// }
if ([self.shareInfo.type isEqualToString:@"TEXT"]) {
if([self.shareInfo.content containsString:@"{"]){
NSDictionary *dic=[self parseJSONStringToNSDictionary:self.shareInfo.content];
//文字内容的Frame
CGFloat contentX=70;
CGFloat contentY=40;
CGFloat contentW=YBScreenBoundsWidth-90;
CGSize size1 = CGSizeMake(YBScreenBoundsWidth-90, MAXFLOAT);
NSDictionary *dict = @{NSFontAttributeName:ContentFont};
CGSize size2 =[[NSString stringWithFormat:@"%@%@",dic[@"user"][@"name"],dic[@"message"]] boundingRectWithSize:size1 options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
CGFloat contentH = size2.height;
self.contentFrame=CGRectMake(contentX, contentY, contentW, contentH);
// if (contentH>90) {
// if (!self.shareInfo.hasShowAll) {
// self.contentFrame=CGRectMake(contentX, contentY, contentW, 90);
// }
// }
}
self.timeFrame=CGRectMake(70, CGRectGetMaxY(self.contentFrame)+10, 60,20);
self.blogToolBarF=CGRectMake(YBScreenBoundsWidth-20-22, CGRectGetMaxY(self.contentFrame)+10, 22, 13);
NSMutableString *mstr=[[NSMutableString alloc]initWithCapacity:0];
for (int i=0; i<self.shareInfo.zans.allValues.count; i++) {
if (i==0) {
[mstr appendString:[NSString stringWithFormat:@"❤️%@ ",self.shareInfo.zans.allValues[0]]];
}else{
[mstr appendString:[NSString stringWithFormat:@" %@",self.shareInfo.zans.allValues[i]]];
}
}
CGSize size3 = CGSizeMake(YBScreenBoundsWidth-90, MAXFLOAT);
NSDictionary *dict = @{NSFontAttributeName:ResponseContentFont};
CGSize size4 = [mstr boundingRectWithSize:size3 options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:dict context:nil].size;
self.zanFrame=CGRectMake(70,CGRectGetMaxY(self.timeFrame), YBScreenBoundsWidth-90, size4.height);
NSArray *listArr=self.shareInfo.replyPage[@"list"];
if (![listArr isKindOfClass:[NSNull class]]) {
for (int i=0; i<listArr.count; i++) {
if (i==0) {
NSDictionary *dic=listArr[i];
NSString* strAfterDecodeByUTF8AndURI = [dic[@"content"] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *contentstr=[NSString stringWithFormat:@"%@回复:%@",dic[@"replyer"][@"name"], strAfterDecodeByUTF8AndURI];
CGFloat contentRepX=70;
CGFloat contentRepY;
if (self.shareInfo.zans.allKeys.count==0) {
contentRepY=CGRectGetMaxY(self.timeFrame)+10;
}else{
contentRepY=CGRectGetMaxY(self.zanFrame)+10;
}
CGFloat contentRepW=YBScreenBoundsWidth-90;
CGSize size3 = CGSizeMake(YBScreenBoundsWidth-90, MAXFLOAT);
NSDictionary *dict = @{NSFontAttributeName:ResponseContentFont};
CGSize size4 = [contentstr boundingRectWithSize:size3 options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:dict context:nil].size;
CGFloat contentRepH=size4.height;
CGRect rect=CGRectMake(contentRepX, contentRepY, contentRepW, contentRepH);
NSValue *value = [NSValue valueWithCGRect:rect];
[self.responseViewHeightArr addObject:value];
}else{
NSDictionary *dic=listArr[i];
NSValue *value=[self.responseViewHeightArr objectAtIndex:i-1];
CGRect lastrect=[value CGRectValue];
NSString* strAfterDecodeByUTF8AndURI = [dic[@"content"] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *contentstr=[NSString stringWithFormat:@"%@回复:%@",dic[@"replyer"][@"name"],strAfterDecodeByUTF8AndURI];
CGFloat contentRepX=70;
CGFloat contentRepY=CGRectGetMaxY(lastrect)+10;
CGFloat contentRepW=YBScreenBoundsWidth-90;
CGSize size3 = CGSizeMake(YBScreenBoundsWidth-90, MAXFLOAT);
NSDictionary *dict = @{NSFontAttributeName:ResponseContentFont};
CGSize size4 = [contentstr boundingRectWithSize:size3 options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:dict context:nil].size;
CGFloat contentRepH=size4.height;
CGRect rect=CGRectMake(contentRepX, contentRepY, contentRepW, contentRepH);
NSValue *value2 = [NSValue valueWithCGRect:rect];
[self.responseViewHeightArr addObject:value2];
}
}
}
}
if ([self.shareInfo.type isEqualToString:@"SOUND"]) {
// if ([self.shareInfo.duration intValue]<16) {
// self.playSoundFrame=CGRectMake(70,CGRectGetMaxY(self.contentFrame)+10, (YBScreenBoundsWidth-90)/16*[self.shareInfo.duration intValue], 25);
// }else{
// self.playSoundFrame=CGRectMake(70,CGRectGetMaxY(self.contentFrame)+10, (YBScreenBoundsWidth-90), 25);
// }
self.playSoundFrame=CGRectMake(70, CGRectGetMaxY(self.contentFrame),100, 25);
self.timeFrame=CGRectMake(70, CGRectGetMaxY(self.playSoundFrame)+10, 100,20);
self.blogToolBarF=CGRectMake(YBScreenBoundsWidth-20-22, CGRectGetMaxY(self.playSoundFrame)+10, 22, 13);
NSMutableString *mstr=[[NSMutableString alloc]initWithCapacity:0];
for (int i=0; i<self.shareInfo.zans.allValues.count; i++) {
if (i==0) {
[mstr appendString:[NSString stringWithFormat:@"❤️%@ ",self.shareInfo.zans.allValues[0]]];
}else{
[mstr appendString:[NSString stringWithFormat:@" %@",self.shareInfo.zans.allValues[i]]];
}
}
CGSize size3 = CGSizeMake(YBScreenBoundsWidth-90, MAXFLOAT);
NSDictionary *dict = @{NSFontAttributeName:ResponseContentFont};
CGSize size4 = [mstr boundingRectWithSize:size3 options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:dict context:nil].size;
self.zanFrame=CGRectMake(70,CGRectGetMaxY(self.timeFrame), YBScreenBoundsWidth-90, size4.height);
NSArray *listArr=self.shareInfo.replyPage[@"list"];
if (![listArr isKindOfClass:[NSNull class]]) {
for (int i=0; i<listArr.count; i++) {
if (i==0) {
NSDictionary *dic=listArr[i];
NSString* strAfterDecodeByUTF8AndURI = [dic[@"content"] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *contentstr=[NSString stringWithFormat:@"%@回复:%@",dic[@"replyer"][@"name"], strAfterDecodeByUTF8AndURI];
CGFloat contentRepX=70;
CGFloat contentRepY;
if (self.shareInfo.zans.allKeys.count==0) {
contentRepY=CGRectGetMaxY(self.timeFrame)+10;
}else{
contentRepY=CGRectGetMaxY(self.zanFrame)+10;
}
CGFloat contentRepW=YBScreenBoundsWidth-90;
CGSize size3 = CGSizeMake(YBScreenBoundsWidth-90, MAXFLOAT);
NSDictionary *dict = @{NSFontAttributeName:ResponseContentFont};
CGSize size4 = [contentstr boundingRectWithSize:size3 options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:dict context:nil].size;
CGFloat contentRepH=size4.height;
CGRect rect=CGRectMake(contentRepX, contentRepY, contentRepW, contentRepH);
NSValue *value = [NSValue valueWithCGRect:rect];
[self.responseViewHeightArr addObject:value];
}else{
NSDictionary *dic=listArr[i];
NSValue *value=[self.responseViewHeightArr objectAtIndex:i-1];
CGRect lastrect=[value CGRectValue];
NSString* strAfterDecodeByUTF8AndURI = [dic[@"content"] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *contentstr=[NSString stringWithFormat:@"%@回复:%@",dic[@"replyer"][@"name"],strAfterDecodeByUTF8AndURI];
CGFloat contentRepX=70;
CGFloat contentRepY=CGRectGetMaxY(lastrect)+10;
CGFloat contentRepW=YBScreenBoundsWidth-90;
CGSize size3 = CGSizeMake(YBScreenBoundsWidth-90, MAXFLOAT);
NSDictionary *dict = @{NSFontAttributeName:ResponseContentFont};
CGSize size4 = [contentstr boundingRectWithSize:size3 options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:dict context:nil].size;
CGFloat contentRepH=size4.height;
CGRect rect=CGRectMake(contentRepX, contentRepY, contentRepW, contentRepH);
NSValue *value2 = [NSValue valueWithCGRect:rect];
[self.responseViewHeightArr addObject:value2];
}
}
}
}
if ([self.shareInfo.type isEqualToString:@"LOCATION"]) {
self.mapFrame=CGRectMake(70,40, YBScreenBoundsWidth-90, YBScreenBoundsWidth-90);
self.timeFrame=CGRectMake(70, CGRectGetMaxY(self.mapFrame)+10, 100,20);
self.blogToolBarF=CGRectMake(YBScreenBoundsWidth-20-22, CGRectGetMaxY(self.mapFrame)+10, 22, 13);
NSMutableString *mstr=[[NSMutableString alloc]initWithCapacity:0];
for (int i=0; i<self.shareInfo.zans.allValues.count; i++) {
if (i==0) {
[mstr appendString:[NSString stringWithFormat:@"❤️%@ ",self.shareInfo.zans.allValues[0]]];
}else{
[mstr appendString:[NSString stringWithFormat:@" %@",self.shareInfo.zans.allValues[i]]];
}
}
CGSize size3 = CGSizeMake(YBScreenBoundsWidth-90, MAXFLOAT);
NSDictionary *dict = @{NSFontAttributeName:ResponseContentFont};
CGSize size4 = [mstr boundingRectWithSize:size3 options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:dict context:nil].size;
self.zanFrame=CGRectMake(70,CGRectGetMaxY(self.timeFrame), YBScreenBoundsWidth-90, size4.height);
NSArray *listArr=self.shareInfo.replyPage[@"list"];
if(![listArr isKindOfClass:[NSNull class]]){
for (int i=0; i<listArr.count; i++) {
if (i==0) {
NSDictionary *dic=listArr[i];
NSString* strAfterDecodeByUTF8AndURI = [dic[@"content"] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *contentstr=[NSString stringWithFormat:@"%@回复:%@",dic[@"replyer"][@"name"], strAfterDecodeByUTF8AndURI];
CGFloat contentRepX=70;
CGFloat contentRepY;
if (self.shareInfo.zans.allKeys.count==0) {
contentRepY=CGRectGetMaxY(self.timeFrame)+10;
}else{
contentRepY=CGRectGetMaxY(self.zanFrame)+10;
}
CGFloat contentRepW=YBScreenBoundsWidth-90;
CGSize size3 = CGSizeMake(YBScreenBoundsWidth-90, MAXFLOAT);
NSDictionary *dict = @{NSFontAttributeName:ResponseContentFont};
CGSize size4 = [contentstr boundingRectWithSize:size3 options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:dict context:nil].size;
CGFloat contentRepH=size4.height;
CGRect rect=CGRectMake(contentRepX, contentRepY, contentRepW, contentRepH);
NSValue *value = [NSValue valueWithCGRect:rect];
[self.responseViewHeightArr addObject:value];
}else{
NSDictionary *dic=listArr[i];
NSValue *value=[self.responseViewHeightArr objectAtIndex:i-1];
CGRect lastrect=[value CGRectValue];
NSString* strAfterDecodeByUTF8AndURI = [dic[@"content"] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *contentstr=[NSString stringWithFormat:@"%@回复:%@",dic[@"replyer"][@"name"],strAfterDecodeByUTF8AndURI];
CGFloat contentRepX=70;
CGFloat contentRepY=CGRectGetMaxY(lastrect)+10;
CGFloat contentRepW=YBScreenBoundsWidth-90;
CGSize size3 = CGSizeMake(YBScreenBoundsWidth-90, MAXFLOAT);
NSDictionary *dict = @{NSFontAttributeName:ResponseContentFont};
CGSize size4 = [contentstr boundingRectWithSize:size3 options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:dict context:nil].size;
CGFloat contentRepH=size4.height;
CGRect rect=CGRectMake(contentRepX, contentRepY, contentRepW, contentRepH);
NSValue *value2 = [NSValue valueWithCGRect:rect];
[self.responseViewHeightArr addObject:value2];
}
}
}
}
if ([self.shareInfo.type isEqualToString:@"VIDEO"]) {
//视频
CGFloat contentVidX=70;
CGFloat contentVidY=40+size2.height+10;
CGFloat contentVidW=YBScreenBoundsWidth-90;
CGFloat contentVidH=150;
self.playviewFrame=CGRectMake(contentVidX, contentVidY, contentVidW, contentVidH);
self.timeFrame=CGRectMake(70, CGRectGetMaxY(self.playviewFrame)+10, 100,20);
self.blogToolBarF=CGRectMake(YBScreenBoundsWidth-20-22, CGRectGetMaxY(self.playviewFrame)+10, 22, 13);
NSMutableString *mstr=[[NSMutableString alloc]initWithCapacity:0];
for (int i=0; i<self.shareInfo.zans.allValues.count; i++) {
if (i==0) {
[mstr appendString:[NSString stringWithFormat:@"❤️%@ ",self.shareInfo.zans.allValues[0]]];
}else{
[mstr appendString:[NSString stringWithFormat:@" %@",self.shareInfo.zans.allValues[i]]];
}
}
CGSize size3 = CGSizeMake(YBScreenBoundsWidth-90, MAXFLOAT);
NSDictionary *dict = @{NSFontAttributeName:ResponseContentFont};
CGSize size4 = [mstr boundingRectWithSize:size3 options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:dict context:nil].size;
self.zanFrame=CGRectMake(70,CGRectGetMaxY(self.timeFrame), YBScreenBoundsWidth-90, size4.height);
NSArray *listArr=self.shareInfo.replyPage[@"list"];
if (![listArr isKindOfClass:[NSNull class]]) {
for (int i=0; i<listArr.count; i++) {
if (i==0) {
NSDictionary *dic=listArr[i];
NSString* strAfterDecodeByUTF8AndURI = [dic[@"content"] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *contentstr=[NSString stringWithFormat:@"%@回复:%@",dic[@"replyer"][@"name"], strAfterDecodeByUTF8AndURI];
CGFloat contentRepX=70;
CGFloat contentRepY;
if (self.shareInfo.zans.allKeys.count==0) {
contentRepY=CGRectGetMaxY(self.timeFrame)+10;
}else{
contentRepY=CGRectGetMaxY(self.zanFrame)+10;
}
CGFloat contentRepW=YBScreenBoundsWidth-90;
CGSize size3 = CGSizeMake(YBScreenBoundsWidth-90, MAXFLOAT);
NSDictionary *dict = @{NSFontAttributeName:ResponseContentFont};
CGSize size4 = [contentstr boundingRectWithSize:size3 options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:dict context:nil].size;
CGFloat contentRepH=size4.height;
CGRect rect=CGRectMake(contentRepX, contentRepY, contentRepW, contentRepH);
NSValue *value = [NSValue valueWithCGRect:rect];
[self.responseViewHeightArr addObject:value];
}else{
NSDictionary *dic=listArr[i];
NSValue *value=[self.responseViewHeightArr objectAtIndex:i-1];
CGRect lastrect=[value CGRectValue];
NSString* strAfterDecodeByUTF8AndURI = [dic[@"content"] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *contentstr=[NSString stringWithFormat:@"%@回复:%@",dic[@"replyer"][@"name"],strAfterDecodeByUTF8AndURI];
CGFloat contentRepX=70;
CGFloat contentRepY=CGRectGetMaxY(lastrect)+10;
CGFloat contentRepW=YBScreenBoundsWidth-90;
CGSize size3 = CGSizeMake(YBScreenBoundsWidth-90, MAXFLOAT);
NSDictionary *dict = @{NSFontAttributeName:ResponseContentFont};
CGSize size4 = [contentstr boundingRectWithSize:size3 options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:dict context:nil].size;
CGFloat contentRepH=size4.height;
CGRect rect=CGRectMake(contentRepX, contentRepY, contentRepW, contentRepH);
NSValue *value2 = [NSValue valueWithCGRect:rect];
[self.responseViewHeightArr addObject:value2];
}
}
}
}
if ([self.shareInfo.type isEqualToString:@"IMAGE"]) {
//图片
for (int i=0; i<self.shareInfo.referUris.count; i++) {
CGFloat imageW=(YBScreenBoundsWidth-110)/3;
CGFloat imageH=imageW;
CGFloat imageX=70+(imageW+10)*(i%3);
CGFloat imageY=10+CGRectGetMaxY(self.contentFrame)+(imageH+10)*(i/3);
CGRect imageRect=CGRectMake(imageX, imageY, imageW, imageH);
NSValue *value = [NSValue valueWithCGRect:imageRect];
[self.imageViewFArr addObject:value];
if (i==self.shareInfo.referUris.count-1) {
NSArray *listArr=self.shareInfo.replyPage[@"list"];
if (![listArr isKindOfClass:[NSNull class]]) {
for (int i=0; i<listArr.count; i++) {
if (i==0) {
NSDictionary *dic=listArr[i];
NSString* strAfterDecodeByUTF8AndURI = [dic[@"content"] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *contentstr=[NSString stringWithFormat:@"%@回复:%@",dic[@"replyer"][@"name"],strAfterDecodeByUTF8AndURI];
CGFloat contentRepX=70;
CGFloat contentRepY=imageY+imageH+10+40;
CGFloat contentRepW=YBScreenBoundsWidth-90;
CGSize size3 = CGSizeMake(YBScreenBoundsWidth-90, MAXFLOAT);
NSDictionary *dict = @{NSFontAttributeName:ResponseContentFont};
CGSize size4 = [contentstr boundingRectWithSize:size3 options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:dict context:nil].size;
CGFloat contentRepH=size4.height;
CGRect rect=CGRectMake(contentRepX, contentRepY, contentRepW, contentRepH);
NSValue *value = [NSValue valueWithCGRect:rect];
[self.responseViewHeightArr addObject:value];
}else{
NSDictionary *dic=listArr[i];
NSValue *value=[self.responseViewHeightArr objectAtIndex:i-1];
CGRect lastrect=[value CGRectValue];
NSString* strAfterDecodeByUTF8AndURI = [dic[@"content"] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *contentstr=[NSString stringWithFormat:@"%@回复:%@",dic[@"replyer"][@"name"],strAfterDecodeByUTF8AndURI];
CGFloat contentRepX=70;
CGFloat contentRepY=CGRectGetMaxY(lastrect)+10;
CGFloat contentRepW=YBScreenBoundsWidth-90;
CGSize size3 = CGSizeMake(YBScreenBoundsWidth-90, MAXFLOAT);
NSDictionary *dict = @{NSFontAttributeName:ResponseContentFont};
CGSize size4 = [contentstr boundingRectWithSize:size3 options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:dict context:nil].size;
CGFloat contentRepH=size4.height;
CGRect rect=CGRectMake(contentRepX, contentRepY, contentRepW, contentRepH);
NSValue *value2 = [NSValue valueWithCGRect:rect];
[self.responseViewHeightArr addObject:value2];
}
}
}
if (i==self.shareInfo.referUris.count-1) {
NSValue *lasevalue=[self.imageViewFArr lastObject];
CGRect rect=[lasevalue CGRectValue];
self.timeFrame=CGRectMake(70, CGRectGetMaxY(rect)+10, 150,20);
self.blogToolBarF=CGRectMake(YBScreenBoundsWidth-20-22, CGRectGetMaxY(rect)+6,22, 13);
NSMutableString *mstr=[[NSMutableString alloc]initWithCapacity:0];
for (int i=0; i<self.shareInfo.zans.allValues.count; i++) {
if (i==0) {
[mstr appendString:[NSString stringWithFormat:@"❤️%@",self.shareInfo.zans.allValues[0]]];
}else{
[mstr appendString:self.shareInfo.zans.allValues[i]];
}
}
CGSize size3 = CGSizeMake(YBScreenBoundsWidth-90, MAXFLOAT);
NSDictionary *dict = @{NSFontAttributeName:ResponseContentFont};
CGSize size4 = [mstr boundingRectWithSize:size3 options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:dict context:nil].size;
self.zanFrame=CGRectMake(70,CGRectGetMaxY(self.timeFrame), YBScreenBoundsWidth-90, size4.height);
}
}
}
}
self.delectFrame=CGRectMake(150, self.timeFrame.origin.y-2, 60, 25);
if (contentH>90&&![self.shareInfo.content containsString:@"{"]) {
self.showAllFrame=CGRectMake(210, self.timeFrame.origin.y-2, 60, 25);
}
NSArray *listArr=self.shareInfo.replyPage[@"list"];
if (![listArr isKindOfClass:[NSNull class]]) {
NSValue *lasevalue=[self.responseViewHeightArr lastObject];
CGRect rect=[lasevalue CGRectValue];
self.cellHeight=CGRectGetMaxY(rect)+20;
}else{
if (self.shareInfo.zans.allKeys.count==0) {
self.cellHeight=CGRectGetMaxY(self.timeFrame)+20;
}else{
self.cellHeight=CGRectGetMaxY(self.zanFrame)+20;
}
}
if (listArr.count==0) {
if (self.shareInfo.zans.allKeys.count==0) {
self.cellHeight=CGRectGetMaxY(self.timeFrame)+20;
}else{
self.cellHeight=CGRectGetMaxY(self.zanFrame)+20;
}
}
}
@end
3.cell对应的UI搭建
-(void)setShareF:(ShareInfoFrame *)shareF{
_shareF=shareF;
UIImageView *iconimage=[[UIImageView alloc]initWithFrame:shareF.iconImageFrame];
NSString *iconUrlStr = [NSString stringWithFormat:@"%@/msp/user/getHeadIcon?uid=%@",ZJURL,shareF.shareInfo.user.idNum];
iconimage.layer.cornerRadius=20;
iconimage.layer.masksToBounds=YES;
if ([shareF.shareInfo.content containsString:@"{"]) {
[iconimage setImage:[UIImage imageNamed:@"FCircle_gonggao"]];
}else{
[iconimage sd_setImageWithURL:iconUrlStr.mj_url placeholderImage:[UIImage imageNamed:@""]];
}
[self addSubview:iconimage];
UITapGestureRecognizer *imagetap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(iconTapDown)];
iconimage.userInteractionEnabled=YES;
[iconimage addGestureRecognizer:imagetap];
UILabel *nameLab=[[UILabel alloc]initWithFrame:shareF.nameFrame];
nameLab.numberOfLines=0;
nameLab.text=shareF.shareInfo.user.name;
[self addSubview:nameLab];
UILabel *contentLab=[[UILabel alloc]initWithFrame:shareF.contentFrame];
[contentLab setFont:ContentFont];
contentLab.numberOfLines=0;
if ([shareF.shareInfo.content containsString:@"{"]) {
NSDictionary *dic=[self parseJSONStringToNSDictionary:shareF.shareInfo.content];
contentLab.text=[NSString stringWithFormat:@"%@%@",dic[@"user"][@"name"],dic[@"message"]];
}else{
contentLab.text=shareF.shareInfo.content;
}
[self addSubview:contentLab];
UIButton *showAllbtn=[[UIButton alloc]initWithFrame:shareF.showAllFrame];
[showAllbtn setTitleColor:[UIColor colorWithHexString:@"556b95"] forState:UIControlStateNormal];
showAllbtn.font=TimeFont;
if (!shareF.shareInfo.hasShowAll) {
[showAllbtn setTitle:@"全文" forState:UIControlStateNormal];
}else{
[showAllbtn setTitle:@"收起" forState:UIControlStateNormal];
}
[showAllbtn addTarget:self action:@selector(showAllBtnDown:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:showAllbtn];
if ([shareF.shareInfo.type isEqualToString:@"SOUND"]) {
UIButton *playSoundBtn=[[UIButton alloc]initWithFrame:shareF.playSoundFrame];
UIImage *image = [UIImage imageNamed:@"FCircle_yuyinplay"];
//顶端盖的高度
CGFloat top = 0;
CGFloat bottom = 0;//低端盖的高度
CGFloat left = 23;//左端盖的高度
CGFloat right =3;//右端盖的高度
UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
//指定为拉伸模式,伸缩后重新赋值
[playSoundBtn setBackgroundImage: [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch] forState:UIControlStateNormal];
[playSoundBtn addTarget:self action:@selector(playSound) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:playSoundBtn];
UILabel *druLab=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(shareF.playSoundFrame), shareF.playSoundFrame.origin.y, 50, 25)];
druLab.text=[NSString stringWithFormat:@"%@s",shareF.shareInfo.duration];
druLab.font=TimeFont;
[self addSubview:druLab];
}
if ([shareF.shareInfo.type isEqualToString:@"VIDEO"]) {
self.moviePlayer = [[MPMoviePlayerController alloc] init];
self.moviePlayer.controlStyle = MPMovieControlStyleNone;
self.moviePlayer.shouldAutoplay = NO;
self.moviePlayer.view.frame = shareF.playviewFrame;
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self.moviePlayer.view action:@selector(movieTapDown)];
[self.moviePlayer.view addGestureRecognizer:tap];
NSString *url= [NSString stringWithFormat:@"%@/msp%@",ZJURL,shareF.shareInfo.referUris[0]];
// moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
self.moviePlayer.contentURL=[NSURL URLWithString: url];
[self addSubview:self.moviePlayer.view];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(movieFinish) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
[self.moviePlayer pause];
self.bgview=[[UIImageView alloc]initWithFrame:shareF.playviewFrame];
[self.bgview sd_setImageWithURL:[NSString stringWithFormat:@"%@/msp%@",ZJURL,shareF.shareInfo.referUris[1]].url];
[self addSubview:self.bgview];
self.playBtn=[[UIButton alloc]initWithFrame:shareF.playviewFrame];
[self.playBtn setImage:[UIImage imageNamed:@"FCircle_play"] forState:UIControlStateNormal];
[self.playBtn addTarget:self action:@selector(playBtnDown) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.playBtn];
// [self.moviePlayer play];
}
if ([shareF.shareInfo.type isEqualToString:@"LOCATION"]) {
NSArray *arr=[shareF.shareInfo.location componentsSeparatedByString:@","];
// self.mapView=[[MAMapView alloc]init];
//
// CLLocationDegrees lat=[arr[0] doubleValue];
// CLLocationDegrees longi=[arr[1] doubleValue];
// CLLocationCoordinate2D cllocation=CLLocationCoordinate2DMake(lat, longi);
// self.mapView.centerCoordinate=cllocation;
// self.mapView.zoomLevel=13;
//
//
// self.mapView.showsUserLocation=YES;
//
//
// ///把地图添加至view
// [self addSubview:self.mapView];
NSURL *url= [NSURL URLWithString: [NSString stringWithFormat:@"%@/msp%@",ZJURL,shareF.shareInfo.referUris[0]]];
UIImageView *imageview=[[UIImageView alloc]initWithFrame:shareF.mapFrame];
[imageview sd_setImageWithURL:url placeholderImage:nil];
imageview.contentMode=UIViewContentModeScaleToFill;
[self addSubview:imageview];
imageview.userInteractionEnabled=YES;
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(maptapDown)];
[imageview addGestureRecognizer:tap];
}
if ([shareF.shareInfo.type isEqualToString:@"IMAGE"]) {
for (int i=0; i<shareF.imageViewFArr.count; i++) {
NSURL *url= [NSURL URLWithString: [NSString stringWithFormat:@"%@/msp%@",ZJURL,shareF.shareInfo.referUris[i]]];
UIImage * img = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
// UIImageView *imageview=[[UIImageView alloc]initWithFrame:[shareF.imageViewFArr[i] CGRectValue]];
// [imageview sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@""]];
// [self addSubview:imageview];
UIButton *btn=[[UIButton alloc]initWithFrame:[shareF.imageViewFArr[i] CGRectValue]];
[btn setImage:img forState:UIControlStateNormal];
btn.tag=i;
[btn addTarget:self action:@selector(showImageBtn:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn];
}
}
self.timeLab=[[UILabel alloc]initWithFrame:shareF.timeFrame];
// timeLab.text=[self changeTime:shareF.shareInfo.sendTime];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:shareF.shareInfo.sendTime];
self.timeLab.text=[self timeSinceDateNewRole:date];
self.timeLab.font=TimeFont;
self.timeLab.hidden=NO;
[self addSubview:self.timeLab];
if ([shareF.shareInfo.user.idNum isEqualToString:[SelfDataModel returnModelBySelectFMDB].idNum]) {
UIButton *delectBtn=[[UIButton alloc]initWithFrame:shareF.delectFrame];
[delectBtn setFont:TimeFont];
[delectBtn setTitle:@"删除" forState:UIControlStateNormal];
[delectBtn addTarget:self action:@selector(delectBtn) forControlEvents:UIControlEventTouchUpInside];
[delectBtn setTitleColor:[UIColor colorWithHexString:@"556b95"] forState:UIControlStateNormal];
[self addSubview:delectBtn];
}
UILabel *zanLab=[[UILabel alloc]initWithFrame:shareF.zanFrame];
NSMutableString *mzanstr=[[NSMutableString alloc]initWithCapacity:0];
for (int i=0; i<self.shareF.shareInfo.zans.allValues.count; i++) {
if (i==0) {
[mzanstr appendString:[NSString stringWithFormat:@"❤️%@ ",self.shareF.shareInfo.zans.allValues[0]]];
}else{
[mzanstr appendString:[NSString stringWithFormat:@" %@",self.shareF.shareInfo.zans.allValues[i]]];
}
}
zanLab.text=mzanstr;
zanLab.numberOfLines=0;
zanLab.font=ResponseContentFont;
[self addSubview:zanLab];
UIButton *commentBtn=[[UIButton alloc]initWithFrame:shareF.blogToolBarF];
// [commentBtn addTarget:self action:@selector(commentBtnDown) forControlEvents:UIControlEventTouchUpInside];
// [commentBtn setImage:[UIImage imageNamed:@"FCircle_pinglun"] forState:UIControlStateNormal];
[commentBtn setBackgroundImage:[UIImage imageNamed:@"FCircle_pinglun"] forState:UIControlStateNormal];
[self addSubview:commentBtn];
UIButton *bigCommentBtn=[[UIButton alloc]initWithFrame:CGRectMake(shareF.blogToolBarF.origin.x-10, shareF.blogToolBarF.origin.y-3,33, 18)];
[bigCommentBtn addTarget:self action:@selector(commentBtnDown) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:bigCommentBtn];
NSArray *listArr=shareF.shareInfo.replyPage[@"list"];
for (int i=0; i<shareF.responseViewHeightArr.count; i++) {
NSDictionary *dic=listArr[i];
// NSString *unicodeStr = [NSString stringWithCString:[dic[@"content"] UTF8String] encoding:NSUnicodeStringEncoding];
NSString* strAfterDecodeByUTF8AndURI = [dic[@"content"] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *contentstr=[NSString stringWithFormat:@"%@回复:%@",dic[@"replyer"][@"name"],strAfterDecodeByUTF8AndURI];
UILabel *repLab=[[UILabel alloc]initWithFrame:[[shareF.responseViewHeightArr objectAtIndex:i] CGRectValue]];
repLab.numberOfLines=0;
repLab.font=ResponseContentFont;
repLab.text=contentstr;
repLab.tag=100+i;
[self addSubview:repLab];
repLab.userInteractionEnabled=YES;
UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressDown:)];
[repLab addGestureRecognizer:longPress];
}
self.commentview=[[[NSBundle mainBundle]loadNibNamed:@"CommentView" owner:nil options:nil]firstObject];
self.commentview.frame=CGRectMake(YBScreenBoundsWidth-62-100, shareF.blogToolBarF.origin.y-10,0 ,30);
self.commentview.layer.masksToBounds=YES;
self.commentview.hidden=YES;
self.commentview.layer.cornerRadius=3;
self.commentview.delegate=self;
NSString *idnum=[SelfDataModel returnModelBySelectFMDB].idNum;
if([self.shareF.shareInfo.zans.allKeys containsObject:idnum]) {
self.commentview.haszanLab.text=@"取消";
}else{
self.commentview.haszanLab.text=@"赞";
}
[self addSubview:self.commentview];
UIView *lineview=[[UIView alloc]initWithFrame:CGRectMake(0, shareF.cellHeight-1, YBScreenBoundsWidth, 1)];
lineview.backgroundColor=RGB(180, 180, 180, 1);
[self.contentView addSubview:lineview];
}
-(void)longPressDown:(UILongPressGestureRecognizer *)longPress{
UILabel *lab=(UILabel *)longPress.view;
int index=lab.tag-100;
NSArray *listArr=self.shareF.shareInfo.replyPage[@"list"];
NSDictionary *dic=listArr[index];
NSString *selectId=dic[@"replyer"][@"id"];
NSString* strAfterDecodeByUTF8AndURI = [dic[@"content"] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *replyId=dic[@"id"];
if ([selectId isEqualToString:[SelfDataModel returnModelBySelectFMDB].idNum]) {
[self.delegate delectInfowithId:selectId andcontent:strAfterDecodeByUTF8AndURI andReplyid:replyId andShareF:self.shareF andrepIndex:index];
}
}
总结:看起来难的东西,只要思路对了就好了