// // paperCollectCell.m // ThePaperHD // // Created by scar1900 on 15/1/5. // Copyright (c) 2015年 scar1900. All rights reserved. // #import "paperCollectCell.h" #import "AsyncImageView.h" @interface paperCollectCell() { CGFloat buttonWidth; CGFloat viewScale; CGFloat timeLabelWidth; CGFloat titleHeight; BOOL isHaveRead; } @property(nonatomic, strong)AsyncImageView *imageView; @property(nonatomic, strong)UIButton *columnButton; @property(nonatomic, strong)UILabel *titleLabel; @property(nonatomic, strong)UIImageView *timeImageView; @property(nonatomic, strong)UILabel *timeLabel; @property(nonatomic, strong)UIImageView *commentImageView; @property(nonatomic, strong)UILabel *commentLabel; @property(nonatomic, strong)UILabel *cornerLabel; @property(nonatomic, strong)UIImageView *itemBorderView; @property(nonatomic, strong)UIImageView *cornerLabelBack; @property(nonatomic, strong)UIImageView *videoView; @property(nonatomic, strong)UIImageView *imageNumView; @property(nonatomic, strong)UIImageView *videoImg; @property(nonatomic, strong)UILabel *videoDurationLabel; @property(nonatomic, strong)UIImageView *imageSetImg; @property(nonatomic, strong)UILabel *imgeSetNumLabel; @end @implementation paperCollectCell @synthesize listContentBO = _listContentBO; @synthesize delegate; - (void)prepareForReuse { [super prepareForReuse]; } - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor clearColor]; viewScale = frame.size.width/300; [self addSubview:self.backView]; [self.backView addSubview:self.imageView]; [self.backView addSubview:self.titleLabel]; [self.backView addSubview:self.columnButton]; [self.backView addSubview:self.timeImageView]; [self.backView addSubview:self.timeLabel]; [self.backView addSubview:self.commentImageView]; [self.backView addSubview:self.commentLabel]; [self.backView addSubview:self.cornerLabelBack]; [self.cornerLabelBack addSubview:self.cornerLabel]; // [self addSubview:self.itemBorderView]; // [self.imageView addSubview:self.videoView]; [self.imageView addSubview:self.imageNumView]; isHaveRead = NO; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshAfterOpenNightMode:) name:REFRESHAFTERNIGHTMODE object:nil]; } return self; } - (UIImageView*)videoView { if (!_videoView) {//【需求】列表页 视频图集水印压图有渐变阴影效果(bug:5972) _videoView = [[UIImageView alloc]initWithFrame:CGRectZero]; // _videoView.backgroundColor = [UIColor colorWithHexString:@"0x203032"]; _videoView.hidden = YES; _videoView.image = Image(@"frontPage/videoAndImageBg.png"); [_videoView addSubview:self.videoImg]; [_videoView addSubview:self.videoDurationLabel]; } return _videoView; } - (UIImageView*)videoImg { if (!_videoImg) { _videoImg = [[UIImageView alloc]initWithImage:Image(@"detailPage/videoIcon.png")]; } return _videoImg; } - (UILabel*)videoDurationLabel { if (!_videoDurationLabel) { _videoDurationLabel = [[UILabel alloc]initWithFrame:CGRectZero]; _videoDurationLabel.font = appFont(13, NO); _videoDurationLabel.textColor = [UIColor whiteColor]; _videoDurationLabel.backgroundColor = [UIColor clearColor]; _videoDurationLabel.textAlignment = NSTextAlignmentCenter; } return _videoDurationLabel; } - (UIImageView*)imageNumView { if (!_imageNumView) {//【需求】列表页 视频图集水印压图有渐变阴影效果(bug:5972) _imageNumView = [[UIImageView alloc]initWithFrame:CGRectZero]; // _imageNumView.backgroundColor = [UIColor clearColor]; _imageNumView.hidden = YES; _imageNumView.image = Image(@"frontPage/videoAndImageBg.png"); [_imageNumView addSubview:self.imageSetImg]; [_imageNumView addSubview:self.imgeSetNumLabel]; } return _imageNumView; } - (UIImageView*)imageSetImg { if (!_imageSetImg) { _imageSetImg = [[UIImageView alloc]initWithImage:Image(@"detailPage/imageListPrint.png")]; } return _imageSetImg; } - (UILabel*)imgeSetNumLabel { if (!_imgeSetNumLabel) { _imgeSetNumLabel = [[UILabel alloc]initWithFrame:CGRectZero]; _imgeSetNumLabel.font = appFont(13, NO); _imgeSetNumLabel.textColor = [UIColor whiteColor]; _imgeSetNumLabel.backgroundColor = [UIColor clearColor]; _imgeSetNumLabel.textAlignment = NSTextAlignmentCenter; } return _imgeSetNumLabel; } - (UIImageView*)itemBorderView { if (!_itemBorderView) { _itemBorderView = [[UIImageView alloc]init]; if ([[TPUserDefault instance].isNightMode intValue] == 0) { _itemBorderView.image = Image(@"frontPage/itemBorder.png"); }else { _itemBorderView.image = Image(@"frontPage/itemBorder_night.png"); } } return _itemBorderView; } - (UIView*)backView { if (!_backView){ _backView = [[UIView alloc]initWithFrame:CGRectZero]; _backView.backgroundColor = [UIColor colorWithHexString:CardBackGroundColor]; } return _backView; } - (AsyncImageView*)imageView { if (!_imageView) { _imageView = [[AsyncImageView alloc]initWithFrame:CGRectZero]; } return _imageView; } - (UILabel*)titleLabel { if (!_titleLabel) { _titleLabel = [[UILabel alloc]initWithFrame:CGRectZero]; _titleLabel.textColor = [UIColor colorWithHexString:TextBlack]; _titleLabel.textAlignment = NSTextAlignmentLeft; _titleLabel.lineBreakMode = NSLineBreakByWordWrapping; _titleLabel.numberOfLines = 0; _titleLabel.font = appFont(18, NO); _titleLabel.backgroundColor = [UIColor clearColor]; } return _titleLabel; } - (UIButton*)columnButton { if (!_columnButton) { _columnButton = [UIButton buttonWithType:UIButtonTypeCustom]; if ([[TPUserDefault instance].isNightMode intValue] == 0) { [_columnButton setBackgroundImage:Image(@"frontPage/columnBack.png") forState:UIControlStateNormal]; [_columnButton setBackgroundImage:Image(@"frontPage/columnBack.png") forState:UIControlStateHighlighted]; }else { [_columnButton setBackgroundImage:Image(@"frontPage/columnBack_night.png") forState:UIControlStateNormal]; [_columnButton setBackgroundImage:Image(@"frontPage/columnBack_night.png") forState:UIControlStateHighlighted]; } [_columnButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [_columnButton setTitleColor:[UIColor colorWithHexString:BUTTONSELECTBACK] forState:UIControlStateHighlighted]; [_columnButton addTarget:self action:@selector(clickColumnBtn:) forControlEvents:UIControlEventTouchUpInside]; _columnButton.titleLabel.font = appFont(11, NO); } return _columnButton; } - (UIImageView*)timeImageView { if (!_timeImageView) { _timeImageView = [[UIImageView alloc]initWithFrame:CGRectZero]; if ([[TPUserDefault instance].isNightMode intValue] == 0) { _timeImageView.image = Image(@"frontPage/time_img.png"); }else { _timeImageView.image = Image(@"frontPage/time_img_night.png"); } _timeImageView.hidden = YES; } return _timeImageView; } - (UILabel*)timeLabel { if (!_timeLabel) { _timeLabel = [[UILabel alloc]initWithFrame:CGRectZero]; _timeLabel.textAlignment = NSTextAlignmentLeft; _timeLabel.textColor = [UIColor colorWithHexString:TextGray]; _timeLabel.font = appFont(11, NO); _timeLabel.backgroundColor = [UIColor clearColor]; } return _timeLabel; } - (UILabel*)commentLabel { if (!_commentLabel) { _commentLabel = [[UILabel alloc]initWithFrame:CGRectZero]; _commentLabel.textAlignment = NSTextAlignmentLeft; _commentLabel.textColor = [UIColor colorWithHexString:TextGray]; _commentLabel.font =appFont(11, NO); _commentLabel.backgroundColor = [UIColor clearColor]; } return _commentLabel; } - (UIImageView*)commentImageView { if (!_commentImageView) { _commentImageView = [[UIImageView alloc]initWithFrame:CGRectZero]; if ([[TPUserDefault instance].isNightMode intValue] == 0) { _commentImageView.image = Image(@"frontPage/comment_img.png"); }else { _commentImageView.image = Image(@"frontPage/comment_img_night.png"); } _commentImageView.hidden = YES; } return _commentImageView; } - (UIImageView*)cornerLabelBack { if (!_cornerLabelBack) { _cornerLabelBack = [[UIImageView alloc]initWithFrame:CGRectZero]; _cornerLabelBack.image = Image(@"frontPage/cornerLabelBack.png"); _cornerLabelBack.backgroundColor = [UIColor clearColor]; } return _cornerLabelBack; } - (UILabel*)cornerLabel { if (!_cornerLabel) { _cornerLabel = [[UILabel alloc]initWithFrame:CGRectZero]; _cornerLabel.textAlignment = NSTextAlignmentCenter; _cornerLabel.backgroundColor = [UIColor clearColor]; _cornerLabel.textColor = [UIColor colorWithHexString:BLUECOLOR]; _cornerLabel.font = appFont(10, NO); } return _cornerLabel; } - (void)setListContentBO:(listContObjectVO *)data { if (!_listContentBO) { [self layoutSubviews]; } _listContentBO = data; self.imageView.imageUrl = self.listContentBO.pic; [self operationHandler:_listContentBO]; } //- (void)getImageSuccess:(UIImage *)image { // if (!image) { // // } //} - (void)operationHandler:(listContObjectVO*)listBO { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSDictionary *nodeInfo = listBO.nodeInfo; viewScale = self.bounds.size.width/300; buttonWidth = [self getButtonWidthWithStr:nodeInfo[@"name"]]; titleHeight = heightForString(listBO.name, appFont(18, NO), CGRectGetWidth(self.backView.bounds)-20, NSLineBreakByWordWrapping); timeLabelWidth = widthForString(listBO.pubTime, appFont(11, NO), 10, NSLineBreakByCharWrapping); // dispatch_async(dispatch_get_main_queue(), ^{ // [self updateCellUI:listBO]; // }); NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(readyToReLayout) object:nil]; [[TPUserDefault instance].globalQueue addOperation:operation]; }); } - (void)readyToReLayout { [self performSelectorOnMainThread:@selector(updateCellUI:) withObject:self.listContentBO waitUntilDone:YES]; usleep(1); } - (void)updateCellUI:(listContObjectVO*)listBO { // if (self.commentImageView.hidden && self.timeImageView.hidden) { self.commentImageView.hidden = YES; self.timeImageView.hidden = NO; // } self.titleLabel.text = listBO.name; [self.columnButton setTitle:listBO.nodeInfo[@"name"] forState:UIControlStateNormal]; self.timeLabel.text = listBO.pubTime; if (![listBO.interactionNum isMatchedByRegex:@"k"]) {//【需求】卡片显示:卡片上互动数少于10就不显示互动数这个项目(bug:4310) if([listBO.interactionNum integerValue] <10){ self.commentLabel.hidden = YES; self.commentImageView.hidden = YES; }else{ self.commentLabel.hidden = NO; self.commentImageView.hidden = NO; } }else{ self.commentLabel.hidden = NO; self.commentImageView.hidden = NO; } self.commentLabel.text = listBO.interactionNum; if (isBlankString(listBO.cornerLabelDesc)) { self.cornerLabelBack.hidden = YES; }else { self.cornerLabelBack.hidden = NO; self.cornerLabel.text = listBO.cornerLabelDesc; } if (self.listContentBO.watermark) { if ([self.listContentBO.watermark intValue] == 1) { self.videoView.hidden = NO; self.imageNumView.hidden = YES; self.videoDurationLabel.text = self.listContentBO.duration; }else if ([self.listContentBO.watermark intValue] == 2){ self.videoView.hidden = YES; self.imageNumView.hidden = NO; self.imgeSetNumLabel.text = self.listContentBO.imageNum; }else { self.videoView.hidden = YES; self.imageNumView.hidden = YES; } }else { self.videoView.hidden = YES; self.imageNumView.hidden = YES; } if ([[TPUserDefault instance].isFirstIntoApp intValue] != 1) { if ([self checkIsHaveRead:listBO.forwordNodeId]) { self.titleLabel.textColor = [UIColor colorWithHexString:LIGHTGRAY]; }else { self.titleLabel.textColor = [UIColor colorWithHexString:TextBlack]; } } [self layoutSubviews]; } - (void)clickColumnBtn:(UIButton*)btn { if ([self.delegate respondsToSelector:@selector(goToColumnPage:)]) { [self.delegate goToColumnPage:self.listContentBO]; } } - (CGFloat)getButtonWidthWithStr:(NSString*)str { if (str.length == 3) { return 40; }else if (str.length == 2) { return 40-12; }else if (str.length == 4) { return 40+12; }else if (str.length == 5) { return 40+24; }else return 40+24; } - (void)layoutSubviews { viewScale = self.bounds.size.width/300; self.backView.frame = CGRectMake(1, 1, CGRectGetWidth(self.bounds)-2, 215*viewScale-2); self.itemBorderView.frame = CGRectMake(0, 0, CGRectGetWidth(self.bounds), 215*viewScale); self.imageView.frame = CGRectMake(0, 0, CGRectGetWidth(self.backView.bounds),125*viewScale); self.titleLabel.frame = CGRectMake(10, CGRectGetMaxY(self.imageView.frame)+15/2, CGRectGetWidth(self.backView.bounds)-20, titleHeight); self.columnButton.frame = CGRectMake(CGRectGetMinX(self.titleLabel.frame), CGRectGetHeight(self.backView.bounds)-30, buttonWidth+10, 22); self.timeImageView.frame = CGRectMake(CGRectGetMaxX(self.columnButton.frame)+15, CGRectGetMinY(self.columnButton.frame)+9, 10, 10); self.timeLabel.frame = CGRectMake(CGRectGetMaxX(self.timeImageView.frame)+4, CGRectGetMinY(self.columnButton.frame)+8, timeLabelWidth, 12); self.commentImageView.frame = CGRectMake(CGRectGetMaxX(self.timeLabel.frame)+6, CGRectGetMinY(self.columnButton.frame)+9, 10, 10); self.commentLabel.frame = CGRectMake(CGRectGetMaxX(self.commentImageView.frame)+4, CGRectGetMinY(self.columnButton.frame)+8, 40, 12); if (!self.cornerLabelBack.hidden) { self.cornerLabelBack.frame = CGRectMake(CGRectGetMaxX(self.titleLabel.frame)-30, CGRectGetMinY(self.columnButton.frame)+7, 30, 14); self.cornerLabel.frame = self.cornerLabelBack.bounds; } self.videoView.frame = CGRectMake(CGRectGetWidth(self.imageView.bounds)-88, CGRectGetHeight(self.imageView.bounds)-25, 88, 25); self.videoDurationLabel.frame = CGRectMake(40, 0, 48, 25); self.videoImg.frame = CGRectMake(40-29/2, CGRectGetHeight(self.videoView.frame)/2-29/4, 29/2, 29/2); self.imageNumView.frame = CGRectMake(CGRectGetWidth(self.imageView.bounds)-88, CGRectGetHeight(self.imageView.bounds)-25, 88, 25); self.imgeSetNumLabel.frame = CGRectMake(58, 0, 30, 25); self.imageSetImg.frame = CGRectMake(38, CGRectGetHeight(self.imageNumView.frame)/2-33/4, 20, 33/2); [super layoutSubviews]; } - (BOOL)checkIsHaveRead:(NSString*)contId { BOOL isRead = NO; NSError *error; NSFetchRequest *request = [[NSFetchRequest alloc]init]; //为已创建好的实体利用检索到的上下文创建一个实体描述 NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"HaveReadContentList" inManagedObjectContext:[CoreDataManager shareInstance].managedObjectContext]; [request setEntity:entityDescription]; //确定持久库中是否存在与此字段相对应的托管对象,所以穿件一个谓词来确定字段的正确对象: NSPredicate *pred = [NSPredicate predicateWithFormat:@"contId == %@",contId]; [request setPredicate:pred]; NSArray *objs = [[CoreDataManager shareInstance].managedObjectContext executeFetchRequest:request error:&error]; if (objs == nil) { TPLOG(@"there was an error!!"); } if (objs.count > 0) { isRead = YES; }else isRead = NO; return isRead; } - (void)setSelected:(BOOL)selected { [super setSelected:selected]; self.titleLabel.textColor = [UIColor colorWithHexString:LIGHTGRAY]; } - (void)retinaSelect:(BOOL)highlighted complete:(void(^)())completion{ if (highlighted) { [UIView animateWithDuration:0.05 animations:^{ self.backView.backgroundColor = [UIColor colorWithHexString:@"0xe8e8e8"]; } completion:^(BOOL finished) { if (completion) { completion(); } self.backView.backgroundColor = [UIColor whiteColor]; }]; } } - (void)refreshAfterOpenNightMode:(NSNotification*)noti { self.backView.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; self.titleLabel.textColor = [UIColor colorWithHexString:TextBlack]; if ([[TPUserDefault instance].isNightMode intValue] == 0) { self.itemBorderView.image = Image(@"frontPage/itemBorder.png"); }else { self.itemBorderView.image = Image(@"frontPage/itemBorder_night.png"); } if ([[TPUserDefault instance].isNightMode intValue] == 0) { self.commentImageView.image = Image(@"frontPage/comment_img.png"); }else { self.commentImageView.image = Image(@"frontPage/comment_img_night.png"); } if ([[TPUserDefault instance].isNightMode intValue] == 0) { self.timeImageView.image = Image(@"frontPage/time_img.png"); }else { self.timeImageView.image = Image(@"frontPage/time_img_night.png"); } if ([[TPUserDefault instance].isNightMode intValue] == 0) { [self.columnButton setBackgroundImage:Image(@"frontPage/columnBack.png") forState:UIControlStateNormal]; [self.columnButton setBackgroundImage:Image(@"frontPage/columnBack.png") forState:UIControlStateHighlighted]; }else { [self.columnButton setBackgroundImage:Image(@"frontPage/columnBack_night.png") forState:UIControlStateNormal]; [self.columnButton setBackgroundImage:Image(@"frontPage/columnBack_night.png") forState:UIControlStateHighlighted]; } if ([self checkIsHaveRead:self.listContentBO.forwordNodeId]) { self.titleLabel.textColor = [UIColor colorWithHexString:LIGHTGRAY]; }else { self.titleLabel.textColor = [UIColor colorWithHexString:TextBlack]; } } - (void)dealloc { [[NSNotificationCenter defaultCenter]removeObserver:self]; } @end