// // hotAnswerContentCell.m // ThePaperHD // // Created by scar1900 on 15/1/28. // Copyright (c) 2015年 scar1900. All rights reserved. // #import "messageAnswerCell.h" @interface messageAnswerCell(){ NSString *hostId; } @property(nonatomic, strong)UIButton *ansImg; @end @implementation messageAnswerCell @synthesize commentBO = _commentBO; @synthesize isHaveCopyMenu = _isHaveCopyMenu; @synthesize isNeedTizhu = _isNeedTizhu; @synthesize topicHostId = _topicHostId; @synthesize isNeedAsImg = _isNeedAsImg; - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { self.backgroundColor = [UIColor colorWithHexString:CardBackGroundColor]; self.selectionStyle = UITableViewCellSelectionStyleNone; self.clipsToBounds = YES; _isNeedTizhu = YES; self.headPicImgView.hidden = YES; self.vipImg.hidden = YES; [self addSubview:self.ansImg]; [self bringSubviewToFront:self.menuButton]; self.isHaveCopyMenu = YES; } return self; } - (void)setIsHaveCopyMenu:(BOOL)menu { _isHaveCopyMenu = menu; if (menu) { self.menuButton.hidden = NO; }else { self.menuButton.hidden = YES; } } -(void)setIsNeedTizhu:(BOOL)tizhu{ _isNeedTizhu = tizhu; } -(void)setIsNeedAsImg:(BOOL)isNeed{ _isNeedAsImg = isNeed; self.ansImg.hidden = _isNeedAsImg; } -(void)setTopicHostId:(NSString *)hosId{ _topicHostId = hosId; } - (void)setCommentBO:(commentObjectVO *)data { _commentBO = data; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ userBO *user = setJsonDicToDataModel(data.userInfo, [userBO class]); NSString *nameStr; if([user.userId intValue] == [[TPUserDefault instance].userBO.userId intValue]){ nameStr = @"我:"; }else{ nameStr = user.sname?[NSString stringWithFormat:@"%@:",user.sname]:@""; } self.answerNameHeight = heightForString(nameStr, self.answerNameLabel.font, 400, self.answerNameLabel.lineBreakMode); CGFloat trueContentHeight = _commentBO.labelHeight; if (!data.isExpand || [data.isExpand intValue] == 0) { self.answerContentHeight = trueContentHeight>interaction4LinesSpace?interaction4LinesSpace:trueContentHeight; }else { self.answerContentHeight = trueContentHeight; } NSString *praiseNum = data.praiseTimes; dispatch_async(dispatch_get_main_queue(), ^{ self.answerNameLabel.text = nameStr; self.answerContentLabel.attributedText = getLineSpaceAttributedString(data.content, 10, appFont(interactionFontSize, NO)); if (trueContentHeight > interaction4LinesSpace) { self.expandBtn.hidden = NO; }else{ self.expandBtn.hidden = YES; } self.timeLabel.text = data.pubTime; if (!isBlankString(praiseNum)) { self.praiseButton.titleText = praiseNum; } if (data.isPraised && [data.isPraised intValue] == 1) { [self.praiseButton setSelect:YES animation:NO]; self.praiseButton.userInteractionEnabled = NO; }else { [self.praiseButton setSelect:NO animation:NO]; self.praiseButton.userInteractionEnabled = YES; } [self layoutSubviews]; }); }); } #pragma mark -- view -(UIButton *)ansImg{ if (!_ansImg) { _ansImg = [UIButton buttonWithType:UIButtonTypeCustom]; [_ansImg setBackgroundImage:Image(@"detailPage/message_answer.png") forState:UIControlStateNormal]; [_ansImg setBackgroundImage:Image(@"detailPage/message_answer_h.png") forState:UIControlStateHighlighted]; [_ansImg addTarget:self action:@selector(gotoAnswer:) forControlEvents:UIControlEventTouchUpInside]; _ansImg.userInteractionEnabled = YES; } return _ansImg; } - (void)expandEvent:(UIButton*)btn { if (!self.commentBO.isExpand || [self.commentBO.isExpand intValue] == 0) { self.commentBO.isExpand = @"1"; }else { self.commentBO.isExpand = @"0"; } CGFloat trueContentHeight= heightForAttributeStringWithLabel(getLineSpaceAttributedString(self.commentBO.content, 10, appFont(interactionFontSize, NO)), messagePopSize.width-30, appFont(interactionFontSize, NO)); if (expandCell) { expandCell([self.commentBO.isExpand boolValue],trueContentHeight); } } - (void)layoutSubviews { [super layoutSubviews]; self.answerNameLabel.frame = CGRectMake(15,15, widthForString(self.answerNameLabel.text, self.answerNameLabel.font, self.answerNameHeight, self.answerNameLabel.lineBreakMode), self.answerNameHeight); self.timeLabel.frame = CGRectMake(CGRectGetMinX(self.answerNameLabel.frame), CGRectGetMaxY(self.answerNameLabel.frame), 100, 16); self.answerContentLabel.frame = CGRectMake(CGRectGetMinX(self.answerNameLabel.frame), CGRectGetMaxY(self.timeLabel.frame)+15, messagePopSize.width -30, self.answerContentHeight); if (!self.commentBO.isExpand || [self.commentBO.isExpand intValue] == 0) { [self.expandBtn setImage:Image(@"detailPage/expandArrow.png") forState:UIControlStateNormal]; }else { [self.expandBtn setImage:Image(@"detailPage/expandArrowUp.png") forState:UIControlStateNormal]; } self.expandBtn.frame = CGRectMake(CGRectGetMinX(self.answerContentLabel.frame), CGRectGetMaxY(self.answerContentLabel.frame)+4, CGRectGetWidth(self.answerContentLabel.frame), 30); [self.expandBtn setImageEdgeInsets:UIEdgeInsetsMake(15-15/2, CGRectGetWidth(self.expandBtn.frame)/2-13, 15-15/2, CGRectGetWidth(self.expandBtn.frame)/2-13)]; self.lineView.frame = CGRectMake(15, CGRectGetHeight(self.bounds)-1, CGRectGetWidth(self.bounds)-30, 1); self.praiseButton.frame = CGRectMake(CGRectGetWidth(self.bounds)-40, CGRectGetMinY(self.answerNameLabel.frame), 25, 45); self.ansImg.frame = CGRectMake(CGRectGetMinX(self.praiseButton.frame)-55, CGRectGetMinY(self.praiseButton.frame)+8, 35, 37); if (self.expandBtn.hidden) { self.menuButton.frame = CGRectMake(CGRectGetMinX(self.answerContentLabel.frame), CGRectGetMinY(self.answerContentLabel.frame), CGRectGetWidth(self.answerContentLabel.frame), CGRectGetMaxY(self.lineView.frame)-CGRectGetMinY(self.answerContentLabel.frame)); }else{ self.menuButton.frame = self.answerContentLabel.frame; } } #pragma mark - menu tap handler - (void)menuTap:(UITapGestureRecognizer*)tap { [self becomeFirstResponder]; UIMenuItem *commentBack = [[UIMenuItem alloc] initWithTitle:@"回复"action:@selector(commentBack:)]; UIMenuItem *copy = [[UIMenuItem alloc] initWithTitle:@"复制"action:@selector(copyText:)]; UIMenuItem *praise = [[UIMenuItem alloc] initWithTitle:@"点赞"action:@selector(praise:)]; // UIMenuItem *delete = [[UIMenuItem alloc] initWithTitle:@"删除"action:@selector(deleteComment:)]; UIMenuController *menu = [UIMenuController sharedMenuController]; if (menu.menuVisible) { [menu setMenuVisible:NO animated:YES]; return; } NSDictionary *userInfo = self.commentBO.userInfo; userBO *user = setJsonDicToDataModel(userInfo, [userBO class]); if ([user.userId longLongValue] == [[TPUserDefault instance].userBO.userId longLongValue]) { [menu setMenuItems:[NSArray arrayWithObjects:commentBack, copy, praise,nil]]; }else { [menu setMenuItems:[NSArray arrayWithObjects:commentBack, copy, praise, nil]]; } [menu setTargetRect:self.answerContentLabel.frame inView:self]; [menu setMenuVisible:YES animated:YES]; } - (BOOL)canPerformAction:(SEL)action withSender:(id)sender { [super canPerformAction:action withSender:sender]; if ( action == @selector(commentBack:) || action == @selector(copyText:) || action == @selector(praise:) || action == @selector(deleteComment:)) { return YES; } else { return NO; } } - (void)commentBack:(id)sender { if ([self.delegate respondsToSelector:@selector(answerForUserBack:closeHidden:)]) { [self.delegate answerForUserBack:self.commentBO closeHidden:YES]; } } -(void)gotoAnswer:(UIButton*)btn{ if ([self.delegate respondsToSelector:@selector(gotoAnswer:)]) { [self.delegate gotoAnswer:_commentBO]; } } @end