// // FKProInfoCell.m // FirstLink // // Created by jack on 16/8/15. // Copyright © 2016年 FirstLink. All rights reserved. // #import "FKProInfoCell.h" #import "FKProDetailViewModel.h" @interface FKProInfoCell () @property (nonatomic, strong) UILabel *headLabel; @property (nonatomic, strong) UILabel *originLabel; @property (nonatomic, strong) UILabel *translateLabel; @property (nonatomic, strong) UILabel *actionLabel; @property (nonatomic, strong) UIButton *arrowBtn; @end @implementation FKProInfoCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { [self addAllSubviews]; self.selectionStyle = UITableViewCellSelectionStyleNone; self.contentView.backgroundColor = [UIColor whiteColor]; } return self; } - (void)addAllSubviews{ [self.contentView addSubview:self.headLabel]; [self.contentView addSubview:self.linkBtn]; [self.contentView addSubview:self.originLabel]; [self.contentView addSubview:self.translateLabel]; [self.contentView addSubview:self.actionLabel]; [self.contentView addSubview:self.arrowBtn]; [self.contentView addSubview:self.actionBtn]; [self.headLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.contentView).offset(10); make.left.equalTo(self.contentView).offset(15); }]; [self.linkBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.contentView); make.right.equalTo(self.contentView); make.centerY.equalTo(self.headLabel); make.height.mas_equalTo(35); }]; [self.originLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.headLabel.mas_bottom).offset(12); make.centerX.equalTo(self.contentView); make.width.mas_equalTo(UISCREENWIDTH - 30); }]; [self.actionLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(self.contentView).offset(- 5); make.top.equalTo(self.originLabel.mas_bottom).offset(15); }]; [self.arrowBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.actionLabel.mas_right).offset(5); make.centerY.equalTo(self.actionLabel); }]; [self.actionBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.originLabel.mas_bottom); make.centerX.equalTo(self.contentView); make.size.mas_equalTo(CGSizeMake(250, 40)); }]; [self.translateLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.actionLabel.mas_bottom).offset(15); make.left.right.equalTo(self.originLabel); }]; } + (CGFloat)heightForUpText:(NSString *)upText downText:(NSString *)downText showTranslate:(BOOL)showTranslate{ BOOL onlyOne = NO; if (![FLStringHelper isValidString:upText] && ![FLStringHelper isValidString:downText]) return 0; // 显示产品信息四个字 if (![FLStringHelper isValidString:upText] || ![FLStringHelper isValidString:downText]) onlyOne = YES; if (onlyOne){ NSString *upTargetText = downText; if ([FLStringHelper isValidString:upText]) upTargetText = upText; CGFloat orginH = [FLStringHelper sizeOfAttributeString:upTargetText lineSpace:2.4 width:UISCREENWIDTH - 30 font:[UIFont systemFontOfSize:13] maxRow:NSIntegerMax].height; return orginH + 60; // 只有一个 } // 两个都存在 CGFloat orginH = [FLStringHelper sizeOfAttributeString:upText lineSpace:2.4 width:UISCREENWIDTH - 30 font:[UIFont systemFontOfSize:13] maxRow:NSIntegerMax].height; if (showTranslate){ CGFloat translateH = [FLStringHelper sizeOfAttributeString:downText lineSpace:2.4 width:UISCREENWIDTH - 30 font:[UIFont systemFontOfSize:13] maxRow:NSIntegerMax].height; return translateH + orginH + 100; } return orginH + 85; } - (void)setOrginStr:(NSString *)orginStr{ if (![FLStringHelper isValidString:orginStr]){ self.originLabel.text = nil; self.originLabel.attributedText = nil; return; } NSAttributedString *attStr = [FLStringHelper attStringWithText:orginStr lineSpace:2.4f]; self.originLabel.attributedText = attStr; } - (void)setTranslateStr:(NSString *)translateStr{ if (![FLStringHelper isValidString:translateStr]){ self.translateLabel.text = nil; self.translateLabel.attributedText = nil; return; } NSAttributedString *attStr = [FLStringHelper attStringWithText:translateStr lineSpace:2.4f]; self.translateLabel.attributedText = attStr; } - (void)setShowTranslate:(BOOL)showTranslate{ self.translateLabel.hidden = showTranslate ? NO : YES; } - (void)fk_configWithViewModel:(id)viewModel indexPath:(NSIndexPath *)indexPath{ if ([viewModel isKindOfClass:[FKProDetailViewModel class]]) { FKProDetailViewModel *detailModel = (FKProDetailViewModel *)viewModel; self.linkBtn.hidden = YES; if ([FLStringHelper isUrlString:detailModel.dataItem.productInfo.linkUrl]) { self.linkBtn.hidden = NO; } NSString *descChinese = detailModel.dataItem.productInfo.descChinese; NSString *descOrginal = detailModel.dataItem.productInfo.descOrginal; BOOL isAllEmpty = NO; BOOL isOnlyOne = NO; if (![FLStringHelper isValidString:descChinese] || ![FLStringHelper isValidString:descOrginal]) isOnlyOne = YES; if (![FLStringHelper isValidString:descChinese] && ![FLStringHelper isValidString:descOrginal]) isAllEmpty = YES; if ([detailModel.dataItem.supplier supplierType] == SupplierTypeOffline) { self.orginStr = descChinese; [self setTranslateActionStaus:YES]; self.showTranslate = NO; return; } if (isOnlyOne ){ // 只有一个 self.orginStr = descOrginal; if ([FLStringHelper isValidString:descChinese]) self.orginStr = descChinese; [self setTranslateActionStaus:YES]; self.showTranslate = NO; } else if (isAllEmpty){ // 都空 [self setTranslateActionStaus:YES]; self.orginStr = nil; self.translateStr = nil; } else { self.orginStr = descChinese; [self setTranslateActionStaus:YES]; self.showTranslate = detailModel.showTranslate;; BOOL selected = NO; NSString *title = @"查看原文"; if (detailModel.showTranslate) { selected = YES; title = @"显示翻译"; } self.actionLabel.text = title; self.arrowBtn.selected = selected; if ([FLStringHelper isValidString:descOrginal]){ [self setTranslateActionStaus:NO]; self.translateStr = descOrginal; } } } } - (void)setTranslateActionStaus:(BOOL)hiden{ self.actionLabel.hidden = hiden; self.arrowBtn.hidden = hiden; self.actionBtn.hidden = hiden; } #pragma mark - property - (UILabel *)headLabel{ if (_headLabel == nil) { _headLabel = [[UILabel alloc]init]; _headLabel.textColor = UIColorFromRGB(0x333333); _headLabel.font = [UIFont systemFontOfSize:14]; _headLabel.text = @"产品信息"; } return _headLabel; } - (UIButton *)linkBtn{ if (_linkBtn == nil) { _linkBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_linkBtn setTitle:@" 商品原链接" forState:UIControlStateNormal]; [_linkBtn setTitleColor:UIColorFromRGB(0x999999) forState:UIControlStateNormal]; [_linkBtn setImage:[UIImage imageNamed:@"link_icon"] forState:UIControlStateNormal]; _linkBtn.titleLabel.font = [UIFont systemFontOfSize:13]; _linkBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 15); _linkBtn.hidden = YES; } return _linkBtn; } - (UILabel *)originLabel{ if (_originLabel == nil) { _originLabel = [[UILabel alloc]init]; _originLabel.textColor = UIColorFromRGB(0x666666); _originLabel.font = [UIFont systemFontOfSize:13]; _originLabel.numberOfLines = 0; [_originLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical]; } return _originLabel; } - (UILabel *)translateLabel{ if (_translateLabel == nil) { _translateLabel = [[UILabel alloc]init]; _translateLabel.textColor = UIColorFromRGB(0x999999); _translateLabel.font = [UIFont systemFontOfSize:13]; _translateLabel.numberOfLines = 0; } return _translateLabel; } - (UIButton *)actionBtn{ if (_actionBtn == nil) { _actionBtn = [UIButton buttonWithType:UIButtonTypeCustom]; } return _actionBtn; } - (UILabel *)actionLabel{ if (_actionLabel == nil) { _actionLabel = [[UILabel alloc]init]; _actionLabel.text = @"查看原文"; _actionLabel.textColor = UIColorFromRGB(0x999999); _actionLabel.font = [UIFont systemFontOfSize:12]; } return _actionLabel; } - (UIButton *)arrowBtn{ if (_arrowBtn == nil) { _arrowBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_arrowBtn setImage:[UIImage imageNamed:@"basket_arrow_down"] forState:UIControlStateNormal]; [_arrowBtn setImage:[UIImage imageNamed:@"basket_arrow_up"] forState:UIControlStateSelected]; _arrowBtn.userInteractionEnabled = NO; } return _arrowBtn; } @end