热更新demo

answerContentCell.m 22KB

    // // answerContentCell.m // ThePaperDemo // // Created by scar1900 on 14-9-28. // Copyright (c) 2014年 scar1900. All rights reserved. // #import "answerContentCell.h" #define LINETAG 10086 @interface answerContentCell() @end @implementation answerContentCell @synthesize commentBO = _commentBO; @synthesize cellHeight; - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { self.selectionStyle = UITableViewCellSelectionStyleNone; self.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR]; [self addSubview:self.backView]; // self.backView.backgroundColor = [UIColor greenColor]; [self.backView addSubview:self.anserNameLabel]; [self.backView addSubview:self.OkButton]; [self.backView addSubview:self.aswerContentLabel]; [self.backView addSubview:self.timeLabel]; [self.backView addSubview:self.timeImg]; [self.backView addSubview:self.menuButton]; [self.backView addSubview:self.expandBtn]; [self.backView addSubview:self.hostLabel]; self.lineView = [[UIView alloc] initWithFrame:CGRectZero]; self.lineView.backgroundColor = [UIColor colorWithHexString:LINECOLOR]; self.lineView.tag = LINETAG; [self.backView addSubview:self.lineView]; [self.backView addSubview:self.commentBack]; [self.commentBack addSubview:self.quoNameLable]; [self.commentBack addSubview:self.quoContentLabel]; [self.backView addSubview:self.answerIcon]; [self.backView addSubview:self.iconBack]; [self.backView addSubview:self.verifyTag]; [self.backView addSubview:self.iconButton]; [self.commentBack addSubview:self.diandiandianLabel]; [self subLayoutSubViews]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(needrefreshNightMode:) name:REFRESHAFTERNIGHTMODE object:nil]; } return self; } - (void)needrefreshNightMode:(id)sender{ self.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR]; _lineView.backgroundColor = [UIColor colorWithHexString:LINECOLOR]; _backView.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; _anserNameLabel.textColor = [UIColor colorWithHexString:TextBlack]; _aswerContentLabel.textColor = [UIColor colorWithHexString:TextGray]; _timeLabel.textColor = [UIColor colorWithHexString:LIGHTGRAY]; _commentBack.layer.borderColor = [UIColor colorWithHexString:LINECOLOR].CGColor; _quoNameLable.textColor = [UIColor colorWithHexString:TextGray]; _quoContentLabel.textColor = [UIColor colorWithHexString:TextGray]; _triangleImg.image = [[TPUserDefault instance].isNightMode intValue] > 0 ?Image(@"other/commentBack_triangle_night.png"):Image(@"other/commentBack_triangle.png"); _iconBack.image = [TPUserDefault instance].isNightMode.intValue >0 ? Image(@"detailPage/icon_night.png") :Image(@"detailPage/icon_sun.png"); _diandiandianLabel.textColor = [UIColor colorWithHexString:TextGray]; [self.quoContentLabel setNeedsDisplay]; [self.aswerContentLabel setNeedsDisplay]; } - (void)dealloc { self.backView = nil; self.anserNameLabel = nil; self.OkButton = nil; self.aswerContentLabel = nil; self.expandBtn = nil; self.commentBack = nil; self.quoContentLabel = nil; self.quoNameLable = nil; [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (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 = user.sname?[NSString stringWithFormat:@"%@:",user.sname]:@""; CGFloat trueContentHeight= returnTextHeightWithRTLabel(data.content, rect_screen.size.width-20-20, appFont(13, NO),7); if (!data.isExpand || [data.isExpand intValue] == 0) { cellHeight = trueContentHeight>getLineHeightWithCountAndFontWidth(4, 10, TEXT_FOUR_LEVELSIZE)?getLineHeightWithCountAndFontWidth(4, 10, TEXT_FOUR_LEVELSIZE):trueContentHeight; }else { cellHeight = trueContentHeight; } NSString *praiseNum = data.praiseTimes; dispatch_async(dispatch_get_main_queue(), ^{ self.anserNameLabel.text = nameStr; self.aswerContentLabel.text = data.content; if (trueContentHeight > getLineHeightWithCountAndFontWidth(4, 10, TEXT_FOUR_LEVELSIZE)) { self.expandBtn.hidden = NO; }else self.expandBtn.hidden = YES; self.timeLabel.text = data.pubTime; if (!isBlankString(praiseNum)) { self.OkButton.text = praiseNum; } if (data.isPraised && [data.isPraised intValue] == 1) { [self.OkButton setSelected:YES]; }else { [self.OkButton setSelected:NO]; } [self reLayoutSubViews]; }); }); // self.anserNameLabel.text = data.userName?[NSString stringWithFormat:@"%@:",data.userName]:@""; self.aswerContentLabel.text = data?data.content:@""; self.OkButton.text = data.praiseTimes; if (data.quoteInfo && data.quoteInfo.allKeys.count>0) { NSDictionary *dic = data.quoteInfo; commentObjectVO *quoBO = setJsonDicToDataModel(dic, [commentObjectVO class]); self.commentBack.hidden = NO; self.commentBack.userInteractionEnabled = YES; self.quoNameLable.hidden = NO; self.quoContentLabel.hidden = NO; self.quoNameLable.text = [NSString stringWithFormat:@"%@:",quoBO.userName]; self.quoContentLabel.text = quoBO.content; self.quoContentHeight = returnTextHeightWithRTLabel(quoBO.content, rect_screen.size.width-20, appFont(13, NO), 5); if (self.quoContentHeight <= getLineHeightWithCountAndFontWidth(2, 10, TEXT_FOUR_LEVELSIZE)) { self.commentBack.userInteractionEnabled = NO; } if (!data.isQuoExpand || [data.isQuoExpand intValue] == 0) { self.quoContentHeight = self.quoContentHeight>getLineHeightWithCountAndFontWidth(2, 10, TEXT_FOUR_LEVELSIZE)?getLineHeightWithCountAndFontWidth(2, 10, TEXT_FOUR_LEVELSIZE):self.quoContentHeight; } }else { self.commentBack.hidden = YES; self.quoNameLable.hidden = YES; self.quoContentLabel.hidden = YES; self.commentBack.userInteractionEnabled = NO; } [self reLayoutSubViews]; } - (UIView *)backView { if (!_backView) { _backView = [[UIView alloc]initWithFrame:CGRectZero]; _backView.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; } return _backView; } - (UIButton *)iconButton { if (!_iconButton) { _iconButton = [[UIButton alloc] initWithFrame:CGRectZero]; [_iconButton addTarget:self action:@selector(iconButtonClick) forControlEvents:UIControlEventTouchUpInside]; } return _iconButton; } - (UIImageView *)verifyTag { if (!_verifyTag) { _verifyTag = [[UIImageView alloc] initWithFrame:CGRectZero]; _verifyTag.image = Image(@"topic/vipVerify.png"); } return _verifyTag; } - (UIImageView *)answerIcon { if (_answerIcon == nil) { _answerIcon = [[UIImageView alloc] initWithFrame:CGRectZero]; _answerIcon.image = Image(@"login/loginIcon_s.png"); } return _answerIcon; } - (UIButton*)menuButton { if (!_menuButton) { _menuButton = [UIButton buttonWithType:UIButtonTypeCustom]; _menuButton.frame = CGRectZero; _menuButton.backgroundColor = [UIColor clearColor]; [_menuButton addTarget:self action:@selector(menuTap:) forControlEvents:UIControlEventTouchUpInside]; } return _menuButton; } - (void)quoExandBtnClick:(UIButton *)btn { if (!self.commentBO.isQuoExpand || [self.commentBO.isQuoExpand intValue] == 0) { self.commentBO.isQuoExpand = @"1"; }else { self.commentBO.isQuoExpand = @"0"; } CGFloat answerContentHeight = self.quoContentLabel.optimumSize.height; if (quoBlock) { quoBlock([self.commentBO.isQuoExpand boolValue],answerContentHeight); } } - (void)quoBlock:(void (^)(BOOL, CGFloat))block { quoBlock = [block copy]; } - (UILabel*)anserNameLabel { if (!_anserNameLabel) { _anserNameLabel = [[UILabel alloc]initWithFrame:CGRectZero]; _anserNameLabel.textColor = [UIColor colorWithHexString:TextBlack]; _anserNameLabel.textAlignment = NSTextAlignmentLeft; _anserNameLabel.lineBreakMode = NSLineBreakByTruncatingTail; _anserNameLabel.font = appFont(TEXT_SIX_LEVELSIZE, NO); } return _anserNameLabel; } - (RTLabel*)aswerContentLabel { if (!_aswerContentLabel) { _aswerContentLabel = [[RTLabel alloc]initWithFrame:CGRectMake(0, 0, rect_screen.size.width-20, 0)]; _aswerContentLabel.textAlignment = RTTextAlignmentLeft; _aswerContentLabel.lineBreakMode = RTTextLineBreakModeWordWrapping; _aswerContentLabel.lineSpacing = 7; _aswerContentLabel.textColor = [UIColor colorWithHexString:TextGray]; _aswerContentLabel.font = appFont(TEXT_FIVE_LEVELSIZE, NO); // _aswerContentLabel.backgroundColor = [UIColor greenColor]; } return _aswerContentLabel; } - (commentOkButton*)OkButton { if (!_OkButton) { _OkButton = [[commentOkButton alloc]initWithFrame:CGRectZero]; [_OkButton addTarget:self action:@selector(commentOK:) forControlEvents:UIControlEventTouchUpInside]; } return _OkButton; } - (UIButton*)expandBtn { if (!_expandBtn) { _expandBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _expandBtn.backgroundColor = [UIColor clearColor]; _expandBtn.hidden = YES; [_expandBtn setImage:Image(@"detailPage/expandArrow.png") forState:UIControlStateNormal]; [_expandBtn addTarget:self action:@selector(expandEvent:) forControlEvents:UIControlEventTouchUpInside]; _expandBtn.selected = NO; // _expandBtn.backgroundColor = [UIColor greenColor]; } return _expandBtn; } - (UILabel *)timeLabel { if (!_timeLabel) { _timeLabel = [[UILabel alloc] initWithFrame:CGRectZero]; _timeLabel.textColor = [UIColor colorWithHexString:LIGHTGRAY]; _timeLabel.backgroundColor = [UIColor clearColor]; _timeLabel.font = appFont(8, NO); } return _timeLabel; } - (UILabel *)diandiandianLabel { if (!_diandiandianLabel) { _diandiandianLabel = [[UILabel alloc] initWithFrame:CGRectZero]; _diandiandianLabel.textColor = [UIColor colorWithHexString:TextGray]; _diandiandianLabel.backgroundColor = [UIColor clearColor]; _diandiandianLabel.font = appFont(15, YES); _diandiandianLabel.text = @"..."; _diandiandianLabel.textAlignment = NSTextAlignmentCenter; _diandiandianLabel.hidden = YES; } return _diandiandianLabel; } - (UIImageView *)iconBack { if (!_iconBack) { _iconBack = [[UIImageView alloc] initWithFrame:CGRectZero]; _iconBack.image = [TPUserDefault instance].isNightMode.intValue >0 ? Image(@"detailPage/icon_night.png") :Image(@"detailPage/icon_sun.png"); } return _iconBack; } - (UIImageView *)timeImg { if (!_timeImg) { _timeImg = [[UIImageView alloc] initWithFrame:CGRectZero]; _timeImg.image = Image(@"topic/timeImg.png"); } return _timeImg; } - (UIButton *)commentBack { if (!_commentBack) { _commentBack = [[UIButton alloc] initWithFrame:CGRectZero]; self.commentBack.layer.cornerRadius = 5; self.commentBack.layer.borderWidth = 0.7; _commentBack.layer.borderColor = [UIColor colorWithHexString:LINECOLOR].CGColor; [_commentBack addTarget:self action:@selector(quoExandBtnClick:) forControlEvents:UIControlEventTouchUpInside]; } return _commentBack; } - (UILabel*)quoNameLable { if (!_quoNameLable) { _quoNameLable = [[UILabel alloc]initWithFrame:CGRectZero]; _quoNameLable.textColor = [UIColor colorWithHexString:TextGray]; _quoNameLable.textAlignment = NSTextAlignmentLeft; } _quoNameLable.font = appFont(11,NO); return _quoNameLable; } - (UIImageView *)triangleImg{ if (!_triangleImg) { _triangleImg = [UIImageView new]; _triangleImg.image = [[TPUserDefault instance].isNightMode intValue] > 0 ?Image(@"other/commentBack_triangle_night.png"):Image(@"other/commentBack_triangle.png"); } return _triangleImg; } - (RTLabel *)quoContentLabel { if (!_quoContentLabel) { _quoContentLabel = [[RTLabel alloc]initWithFrame:CGRectZero]; _quoContentLabel.textColor = [UIColor colorWithHexString:TextGray]; _quoContentLabel.textAlignment = RTTextAlignmentLeft; _quoContentLabel.lineBreakMode = RTTextLineBreakModeWordWrapping; _quoContentLabel.lineSpacing = 5; _quoContentLabel.userInteractionEnabled = NO; // _quoContentLabel.backgroundColor = [UIColor greenColor]; } _quoContentLabel.font = appFont(13,NO); return _quoContentLabel; } - (void)commentOK:(UIButton*)btn { if (btn.selected) { return; } [self.OkButton setSelected:YES animated:YES]; NSDictionary *dic = @{@"commentId":self.commentBO.commentId}; __weak typeof(self) Self = self; [Remote doJsonActionWithBlock:2000 requestUrl:commentPraiseURL parameter:dic withWaitCursor:NO completion:^(BOOL success, NSString *message, id responseData) { if (success) { NSString *praseNumStr = responseData[@"praiseTimes"]; if ([responseData[@"praiseTimes"] integerValue] < 1001) { NSString *praseNum = [NSString stringWithFormat:@"%@",responseData[@"praiseTimes"]]; Self.OkButton.text = praseNum; Self.commentBO.praiseTimes = praseNumStr; TPLOG(@"点赞成功"); } Self.commentBO.isPraised = @"1"; if ([Self.delegate respondsToSelector:@selector(hasPraisedAnswer:withCommentBO:)]) { [Self.delegate hasPraisedAnswer:Self.indexPath withCommentBO:Self.commentBO]; } TPLOG(@"点赞成功"); }else { TPLOG(@"点赞失败"); } }]; } - (UILabel *)hostLabel { if (!_hostLabel) { _hostLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 15, 15)]; _hostLabel.text = @"题主"; _hostLabel.textAlignment = NSTextAlignmentCenter; _hostLabel.backgroundColor = [UIColor colorWithHexString:@"0xec1c24"]; _hostLabel.textColor = [UIColor whiteColor]; _hostLabel.layer.masksToBounds = YES; _hostLabel.layer.cornerRadius = 2; _hostLabel.font = appFont(9, NO); _hostLabel.hidden = YES; } return _hostLabel; } - (void)expandEvent:(UIButton*)btn { if (!self.commentBO.isExpand || [self.commentBO.isExpand intValue] == 0) { self.commentBO.isExpand = @"1"; }else { self.commentBO.isExpand = @"0"; } CGFloat answerContentHeight = self.aswerContentLabel.optimumSize.height; if (expandCell) { expandCell([self.commentBO.isExpand boolValue],answerContentHeight); } } - (void)expandCell:(void (^)(BOOL, CGFloat))block { expandCell = [block copy]; } #pragma mark - menu tap handler - (void)menuTap:(UIButton*)btn { [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:)]; UIMenuController *menu = [UIMenuController sharedMenuController]; [menu setMenuItems:[NSArray arrayWithObjects:copy, praise, nil]]; if (menu.menuVisible) { [menu setMenuVisible:NO animated:YES]; return; } [menu setTargetRect:self.aswerContentLabel.frame inView:self]; [menu setMenuVisible:YES animated:YES]; } #pragma mark - menuDelegate - (BOOL)canBecomeFirstResponder { [super canBecomeFirstResponder]; return YES; } - (BOOL)canPerformAction:(SEL)action withSender:(id)sender { [super canPerformAction:action withSender:sender]; if (action == @selector(copyText:) || action == @selector(praise:)|| action == @selector(deleteComment:)) { return YES; } else { return NO; } } - (void)copyText:(id)sender { UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; pasteboard.string = self.aswerContentLabel.text; ShowMessage(@"复制成功", YES); } - (void)praise:(UIButton *)sender { [self commentOK:self.OkButton]; } - (void)subLayoutSubViews { __weak typeof(self) weakSelf = self; [self.backView makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self); }]; [self.timeImg makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.backView.left).offset(10); make.top.equalTo(weakSelf.anserNameLabel.bottom).offset(6); make.height.mas_equalTo(10); make.width.mas_equalTo(10); }]; [self.timeLabel makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.timeImg.right).offset(5); make.top.equalTo(weakSelf.anserNameLabel.bottom).offset(6); make.height.mas_equalTo(9); make.width.mas_equalTo(rect_screen.size.width/2); }]; [self.OkButton makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.backView.right).offset(-40); make.top.equalTo(weakSelf.anserNameLabel.top).offset(-2); make.width.mas_equalTo(30); make.height.mas_equalTo(35); }]; [self.hostLabel makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.anserNameLabel.right).offset(5); make.top.equalTo(weakSelf.anserNameLabel.top); make.width.mas_equalTo(27); make.height.mas_equalTo(12); }]; [self.menuButton makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(weakSelf.aswerContentLabel); }]; [self.expandBtn makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.backView.left); make.right.equalTo(weakSelf.backView.right); make.top.equalTo(weakSelf.aswerContentLabel.bottom); make.height.mas_equalTo(25); }]; [self.lineView makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.backView.left).offset(10); make.right.equalTo(weakSelf.backView.right).offset(-10); make.top.equalTo(weakSelf.backView.bottom).offset(-6); make.height.mas_equalTo(1); }]; [self.quoNameLable makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.commentBack.left).offset(10); make.top.equalTo(weakSelf.commentBack.top).offset(10); make.width.mas_equalTo(weakSelf.aswerContentLabel.frame.size.width-15); make.height.mas_equalTo(25/2); }]; } - (void)reLayoutSubViews { __weak typeof(self) weakSelf = self; CGFloat answerContentHeight = self.aswerContentLabel.optimumSize.height; CGFloat nameWidth = returnTextWidthWithRTLabel(self.anserNameLabel.text, 12, appFont(11, NO), 0); [self.anserNameLabel remakeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(weakSelf.backView.top).offset(9); make.left.equalTo(weakSelf.backView.left).offset(10); make.width.mas_equalTo(nameWidth+40); make.height.mas_equalTo(12); }]; if (answerContentHeight > getLineHeightWithCountAndFontWidth(4, 10, TEXT_FOUR_LEVELSIZE)) { self.expandBtn.hidden = NO; }else self.expandBtn.hidden = YES; if (!self.commentBO.isExpand || [self.commentBO.isExpand intValue] == 0) { answerContentHeight = answerContentHeight>getLineHeightWithCountAndFontWidth(4, 10, TEXT_FOUR_LEVELSIZE)?getLineHeightWithCountAndFontWidth(4, 10, TEXT_FOUR_LEVELSIZE):answerContentHeight; [_expandBtn setImage:Image(@"detailPage/expandArrow.png") forState:UIControlStateNormal]; }else { [_expandBtn setImage:Image(@"detailPage/expandArrowUp.png") forState:UIControlStateNormal]; } [self.aswerContentLabel remakeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.backView.left).offset(10); make.right.equalTo(weakSelf.backView.right).offset(-10); make.top.equalTo(weakSelf.timeLabel.bottom).offset(10); make.height.mas_equalTo(answerContentHeight); }]; CGFloat backHeight = 0; CGFloat tmp = 0; if (!self.commentBO.isQuoExpand || [self.commentBO.isQuoExpand intValue] == 0) { CGFloat labeHeight = returnTextHeightWithRTLabel(self.quoContentLabel.text, rect_screen.size.width-20-16, appFont(13, NO), 5); if (labeHeight > getLineHeightWithCountAndFontWidth(2, 10, TEXT_FOUR_LEVELSIZE)) { tmp = 15; } backHeight = labeHeight > getLineHeightWithCountAndFontWidth(2, 10, TEXT_FOUR_LEVELSIZE)?getLineHeightWithCountAndFontWidth(2, 10, TEXT_FOUR_LEVELSIZE):labeHeight; backHeight += 30; }else { CGFloat labeHeight = returnTextHeightWithRTLabel(self.quoContentLabel.text, rect_screen.size.width-20-16, appFont(13, NO), 5); tmp = 0; backHeight = labeHeight + 30; } [self.commentBack remakeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.aswerContentLabel.left); make.top.equalTo(weakSelf.aswerContentLabel.bottom); make.width.equalTo(weakSelf.aswerContentLabel.width); make.height.mas_equalTo(backHeight); }]; [self.quoContentLabel remakeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.commentBack.left).offset(8); make.top.equalTo(self.quoNameLable.bottom).offset(5); make.right.equalTo(self.commentBack.right).offset(-8); make.bottom.equalTo(self.commentBack.bottom).offset(-3-tmp); }]; } @end