説明なし

FKProInfoCell.m 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. //
  2. // FKProInfoCell.m
  3. // FirstLink
  4. //
  5. // Created by jack on 16/8/15.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "FKProInfoCell.h"
  9. #import "FKProDetailViewModel.h"
  10. @interface FKProInfoCell ()
  11. @property (nonatomic, strong) UILabel *headLabel;
  12. @property (nonatomic, strong) UILabel *originLabel;
  13. @property (nonatomic, strong) UILabel *translateLabel;
  14. @property (nonatomic, strong) UILabel *actionLabel;
  15. @property (nonatomic, strong) UIButton *arrowBtn;
  16. @end
  17. @implementation FKProInfoCell
  18. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
  19. if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
  20. [self addAllSubviews];
  21. self.selectionStyle = UITableViewCellSelectionStyleNone;
  22. self.contentView.backgroundColor = [UIColor whiteColor];
  23. }
  24. return self;
  25. }
  26. - (void)addAllSubviews{
  27. [self.contentView addSubview:self.headLabel];
  28. [self.contentView addSubview:self.linkBtn];
  29. [self.contentView addSubview:self.originLabel];
  30. [self.contentView addSubview:self.translateLabel];
  31. [self.contentView addSubview:self.actionLabel];
  32. [self.contentView addSubview:self.arrowBtn];
  33. [self.contentView addSubview:self.actionBtn];
  34. [self.headLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  35. make.top.equalTo(self.contentView).offset(10);
  36. make.left.equalTo(self.contentView).offset(15);
  37. }];
  38. [self.linkBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  39. make.top.equalTo(self.contentView);
  40. make.right.equalTo(self.contentView);
  41. make.centerY.equalTo(self.headLabel);
  42. make.height.mas_equalTo(35);
  43. }];
  44. [self.originLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  45. make.top.equalTo(self.headLabel.mas_bottom).offset(12);
  46. make.centerX.equalTo(self.contentView);
  47. make.width.mas_equalTo(UISCREENWIDTH - 30);
  48. }];
  49. [self.actionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  50. make.centerX.equalTo(self.contentView).offset(- 5);
  51. make.top.equalTo(self.originLabel.mas_bottom).offset(15);
  52. }];
  53. [self.arrowBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  54. make.left.equalTo(self.actionLabel.mas_right).offset(5);
  55. make.centerY.equalTo(self.actionLabel);
  56. }];
  57. [self.actionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  58. make.top.equalTo(self.originLabel.mas_bottom);
  59. make.centerX.equalTo(self.contentView);
  60. make.size.mas_equalTo(CGSizeMake(250, 40));
  61. }];
  62. [self.translateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  63. make.top.equalTo(self.actionLabel.mas_bottom).offset(15);
  64. make.left.right.equalTo(self.originLabel);
  65. }];
  66. }
  67. + (CGFloat)heightForUpText:(NSString *)upText
  68. downText:(NSString *)downText
  69. showTranslate:(BOOL)showTranslate{
  70. BOOL onlyOne = NO;
  71. if (![FLStringHelper isValidString:upText] && ![FLStringHelper isValidString:downText]) return 0; // 显示产品信息四个字
  72. if (![FLStringHelper isValidString:upText] || ![FLStringHelper isValidString:downText]) onlyOne = YES;
  73. if (onlyOne){
  74. NSString *upTargetText = downText;
  75. if ([FLStringHelper isValidString:upText]) upTargetText = upText;
  76. CGFloat orginH = [FLStringHelper sizeOfAttributeString:upTargetText
  77. lineSpace:2.4
  78. width:UISCREENWIDTH - 30
  79. font:[UIFont systemFontOfSize:13]
  80. maxRow:NSIntegerMax].height;
  81. return orginH + 60; // 只有一个
  82. }
  83. // 两个都存在
  84. CGFloat orginH = [FLStringHelper sizeOfAttributeString:upText
  85. lineSpace:2.4
  86. width:UISCREENWIDTH - 30
  87. font:[UIFont systemFontOfSize:13]
  88. maxRow:NSIntegerMax].height;
  89. if (showTranslate){
  90. CGFloat translateH = [FLStringHelper sizeOfAttributeString:downText
  91. lineSpace:2.4
  92. width:UISCREENWIDTH - 30
  93. font:[UIFont systemFontOfSize:13]
  94. maxRow:NSIntegerMax].height;
  95. return translateH + orginH + 100;
  96. }
  97. return orginH + 85;
  98. }
  99. - (void)setOrginStr:(NSString *)orginStr{
  100. if (![FLStringHelper isValidString:orginStr]){
  101. self.originLabel.text = nil;
  102. self.originLabel.attributedText = nil;
  103. return;
  104. }
  105. NSAttributedString *attStr = [FLStringHelper attStringWithText:orginStr lineSpace:2.4f];
  106. self.originLabel.attributedText = attStr;
  107. }
  108. - (void)setTranslateStr:(NSString *)translateStr{
  109. if (![FLStringHelper isValidString:translateStr]){
  110. self.translateLabel.text = nil;
  111. self.translateLabel.attributedText = nil;
  112. return;
  113. }
  114. NSAttributedString *attStr = [FLStringHelper attStringWithText:translateStr lineSpace:2.4f];
  115. self.translateLabel.attributedText = attStr;
  116. }
  117. - (void)setShowTranslate:(BOOL)showTranslate{
  118. self.translateLabel.hidden = showTranslate ? NO : YES;
  119. }
  120. - (void)fk_configWithViewModel:(id)viewModel indexPath:(NSIndexPath *)indexPath{
  121. if ([viewModel isKindOfClass:[FKProDetailViewModel class]]) {
  122. FKProDetailViewModel *detailModel = (FKProDetailViewModel *)viewModel;
  123. self.linkBtn.hidden = YES;
  124. if ([FLStringHelper isUrlString:detailModel.dataItem.productInfo.linkUrl]) {
  125. self.linkBtn.hidden = NO;
  126. }
  127. NSString *descChinese = detailModel.dataItem.productInfo.descChinese;
  128. NSString *descOrginal = detailModel.dataItem.productInfo.descOrginal;
  129. BOOL isAllEmpty = NO;
  130. BOOL isOnlyOne = NO;
  131. if (![FLStringHelper isValidString:descChinese] || ![FLStringHelper isValidString:descOrginal]) isOnlyOne = YES;
  132. if (![FLStringHelper isValidString:descChinese] && ![FLStringHelper isValidString:descOrginal]) isAllEmpty = YES;
  133. if ([detailModel.dataItem.supplier supplierType] == SupplierTypeOffline) {
  134. self.orginStr = descChinese;
  135. [self setTranslateActionStaus:YES];
  136. self.showTranslate = NO;
  137. return;
  138. }
  139. if (isOnlyOne ){
  140. // 只有一个
  141. self.orginStr = descOrginal;
  142. if ([FLStringHelper isValidString:descChinese]) self.orginStr = descChinese;
  143. [self setTranslateActionStaus:YES];
  144. self.showTranslate = NO;
  145. } else if (isAllEmpty){
  146. // 都空
  147. [self setTranslateActionStaus:YES];
  148. self.orginStr = nil;
  149. self.translateStr = nil;
  150. } else {
  151. self.orginStr = descChinese;
  152. [self setTranslateActionStaus:YES];
  153. self.showTranslate = detailModel.showTranslate;;
  154. BOOL selected = NO;
  155. NSString *title = @"查看原文";
  156. if (detailModel.showTranslate) {
  157. selected = YES;
  158. title = @"显示翻译";
  159. }
  160. self.actionLabel.text = title;
  161. self.arrowBtn.selected = selected;
  162. if ([FLStringHelper isValidString:descOrginal]){
  163. [self setTranslateActionStaus:NO];
  164. self.translateStr = descOrginal;
  165. }
  166. }
  167. }
  168. }
  169. - (void)setTranslateActionStaus:(BOOL)hiden{
  170. self.actionLabel.hidden = hiden;
  171. self.arrowBtn.hidden = hiden;
  172. self.actionBtn.hidden = hiden;
  173. }
  174. #pragma mark - property
  175. - (UILabel *)headLabel{
  176. if (_headLabel == nil) {
  177. _headLabel = [[UILabel alloc]init];
  178. _headLabel.textColor = UIColorFromRGB(0x333333);
  179. _headLabel.font = [UIFont systemFontOfSize:14];
  180. _headLabel.text = @"产品信息";
  181. }
  182. return _headLabel;
  183. }
  184. - (UIButton *)linkBtn{
  185. if (_linkBtn == nil) {
  186. _linkBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  187. [_linkBtn setTitle:@" 商品原链接" forState:UIControlStateNormal];
  188. [_linkBtn setTitleColor:UIColorFromRGB(0x999999) forState:UIControlStateNormal];
  189. [_linkBtn setImage:[UIImage imageNamed:@"link_icon"] forState:UIControlStateNormal];
  190. _linkBtn.titleLabel.font = [UIFont systemFontOfSize:13];
  191. _linkBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 15);
  192. _linkBtn.hidden = YES;
  193. }
  194. return _linkBtn;
  195. }
  196. - (UILabel *)originLabel{
  197. if (_originLabel == nil) {
  198. _originLabel = [[UILabel alloc]init];
  199. _originLabel.textColor = UIColorFromRGB(0x666666);
  200. _originLabel.font = [UIFont systemFontOfSize:13];
  201. _originLabel.numberOfLines = 0;
  202. [_originLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
  203. }
  204. return _originLabel;
  205. }
  206. - (UILabel *)translateLabel{
  207. if (_translateLabel == nil) {
  208. _translateLabel = [[UILabel alloc]init];
  209. _translateLabel.textColor = UIColorFromRGB(0x999999);
  210. _translateLabel.font = [UIFont systemFontOfSize:13];
  211. _translateLabel.numberOfLines = 0;
  212. }
  213. return _translateLabel;
  214. }
  215. - (UIButton *)actionBtn{
  216. if (_actionBtn == nil) {
  217. _actionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  218. }
  219. return _actionBtn;
  220. }
  221. - (UILabel *)actionLabel{
  222. if (_actionLabel == nil) {
  223. _actionLabel = [[UILabel alloc]init];
  224. _actionLabel.text = @"查看原文";
  225. _actionLabel.textColor = UIColorFromRGB(0x999999);
  226. _actionLabel.font = [UIFont systemFontOfSize:12];
  227. }
  228. return _actionLabel;
  229. }
  230. - (UIButton *)arrowBtn{
  231. if (_arrowBtn == nil) {
  232. _arrowBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  233. [_arrowBtn setImage:[UIImage imageNamed:@"basket_arrow_down"] forState:UIControlStateNormal];
  234. [_arrowBtn setImage:[UIImage imageNamed:@"basket_arrow_up"] forState:UIControlStateSelected];
  235. _arrowBtn.userInteractionEnabled = NO;
  236. }
  237. return _arrowBtn;
  238. }
  239. @end