Nessuna descrizione

FKSelectSpecUnitView.m 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. //
  2. // FKProductSingleSpecView.m
  3. // FirstLink
  4. //
  5. // Created by jack on 16/1/15.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "FKSelectSpecUnitView.h"
  9. //#import "PindanSpecItem.h"
  10. #import "FKBasicSpecItem.h"
  11. #define kProductSpecBtnTag 300
  12. #define kProductSpecBtnFont 13
  13. #define KProductSpecBtnTitleMargin 8
  14. #define kProductSpecBtnHight 27.0f
  15. @interface FKSelectSpecUnitView ()
  16. @property (nonatomic, strong) UILabel *titleLabel;
  17. @property (nonatomic, strong) UIButton *sizeBtn;
  18. @property (nonatomic, strong) UIView *sizeBottonLine;
  19. @property (nonatomic, strong) UIButton *sizeLinkButton;
  20. @end
  21. @implementation FKSelectSpecUnitView
  22. - (instancetype)initWithFrame:(CGRect)frame{
  23. if (self = [super initWithFrame:frame]) {
  24. [self addAllSubviews];
  25. }
  26. return self;
  27. }
  28. - (void)addAllSubviews{
  29. [self addSubview:self.titleLabel];
  30. [self addSubview:self.sizeBtn];
  31. [self addSubview:self.sizeBottonLine];
  32. [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  33. make.top.equalTo(self).offset(15);
  34. make.left.equalTo(self).offset(15);
  35. }];
  36. [self.sizeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  37. make.right.equalTo(self).offset(-60);
  38. make.centerY.equalTo(self.titleLabel);
  39. make.height.mas_equalTo(25);
  40. }];
  41. [self.sizeBottonLine mas_makeConstraints:^(MASConstraintMaker *make) {
  42. make.left.right.equalTo(self.sizeBtn);
  43. make.centerY.equalTo(self.sizeBtn).offset(10);
  44. make.centerX.equalTo(self.sizeBtn);
  45. make.height.mas_equalTo(0.5);
  46. }];
  47. [self addSubview:self.sizeLinkButton];
  48. [self.sizeLinkButton mas_makeConstraints:^(MASConstraintMaker *make) {
  49. make.left.equalTo(self.sizeBtn.mas_right).offset(6);
  50. make.right.equalTo(self);
  51. make.centerY.equalTo(self.sizeBtn);
  52. make.height.mas_equalTo(40);
  53. }];
  54. self.sizeBtn.hidden = YES;
  55. self.sizeBottonLine.hidden = YES;
  56. self.sizeBtn.userInteractionEnabled = NO;
  57. self.sizeLinkButton.hidden = YES;
  58. }
  59. + (CGFloat)contentHeightForSpecItem:(FKProductSpecItem *)specItem{
  60. NSUInteger rowCount = 0;
  61. if (specItem.basicSpecArray.count){
  62. rowCount = [self rowNumberForSepcsArray:specItem.basicSpecArray index:specItem.basicSpecArray.count - 1] + 1;
  63. }
  64. return 15 + 15 + 20 + rowCount * (kProductSpecBtnHight + 10);
  65. }
  66. - (void)setContentForSpecItem:(FKProductSpecItem *)specItem{
  67. self.specItem = specItem;
  68. self.titleLabel.text = specItem.specName;
  69. UIButton *preBtn = nil;
  70. CGFloat rowNum = 0; // 第几行
  71. for (int i = 0; i < specItem.basicSpecArray.count; i++) {
  72. FKBasicSpecItem *basicItem = specItem.basicSpecArray[i];
  73. BOOL selected = basicItem.selected;
  74. UIButton *button = [self createSpecBtnWithTitle:basicItem.name
  75. tag:i + kProductSpecBtnTag
  76. selected:selected
  77. canUse:basicItem.canUse];
  78. [self addSubview:button];
  79. CGFloat btnWidth = [self sizeForText:basicItem.name withFontSize:kProductSpecBtnFont].width + 2 * KProductSpecBtnTitleMargin;
  80. BOOL rowChange = NO; // 判断当前button是否另起一行
  81. NSUInteger curentRowNum = [FKSelectSpecUnitView rowNumberForSepcsArray:specItem.basicSpecArray index:i];
  82. if (curentRowNum > rowNum && i > 0) {
  83. rowChange = YES;
  84. rowNum = curentRowNum;
  85. }
  86. [button mas_makeConstraints:^(MASConstraintMaker *make) {
  87. if (i == 0){
  88. make.left.equalTo(self.titleLabel);
  89. make.top.equalTo(self.titleLabel.mas_bottom).offset(15);
  90. }else if (rowChange && preBtn){
  91. make.left.equalTo(self.titleLabel);
  92. make.top.equalTo(preBtn.mas_bottom).offset(10);
  93. }else if (preBtn){
  94. make.left.equalTo(preBtn.mas_right).offset(10);
  95. make.top.equalTo(preBtn);
  96. }
  97. make.height.mas_equalTo(kProductSpecBtnHight);
  98. make.width.mas_equalTo(btnWidth);
  99. }];
  100. preBtn = button;
  101. }
  102. }
  103. - (void)reloadContent{
  104. for (int i = 0; i < self.specItem.basicSpecArray.count; i++) {
  105. FKBasicSpecItem *basicItem = [self.specItem basicSpecItemAtIndex:i];
  106. UIButton *button = [self viewWithTag:kProductSpecBtnTag + i];
  107. if ([button isKindOfClass:[UIButton class]]) {
  108. [self refresButton:button selected:basicItem.selected canUse:basicItem.canUse];
  109. }
  110. }
  111. }
  112. - (void)clearAllBtnState{
  113. for (UIButton *button in self.subviews) {
  114. if ([button isKindOfClass:[UIButton class]]) button.selected = NO;
  115. }
  116. }
  117. - (UIButton *)createSpecBtnWithTitle:(NSString *)title
  118. tag:(int)tag
  119. selected:(BOOL)selected
  120. canUse:(BOOL)canUse
  121. {
  122. UIColor *titleColor = selected ? UIColorFromRGB(0xf85a5a) : UIColorFromRGB(0x666666);
  123. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  124. button.tag = tag;
  125. // button.userInteractionEnabled = canUse;
  126. button.titleLabel.font = [UIFont systemFontOfSize:kProductSpecBtnFont];
  127. button.layer.borderWidth = 0.5f;
  128. button.layer.borderColor = selected ? [UIColorFromRGB(0xff6362) CGColor] : [UIColorFromRGB(0xcccccc) CGColor];
  129. [button addTarget:self action:@selector(specsBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  130. [button setTitle:title forState:UIControlStateNormal];
  131. [button setTitleColor:titleColor forState:UIControlStateNormal];
  132. [self refresButton:button selected:selected canUse:canUse];
  133. return button;
  134. }
  135. - (void)refresButton:(UIButton *)button
  136. selected:(BOOL)selected
  137. canUse:(BOOL)canUse{
  138. button.userInteractionEnabled = canUse;
  139. if (!canUse){
  140. [button setBackgroundColor:UIColorFromRGB(0xeeeeee)];
  141. [button setTitleColor:UIColorFromRGB(0xcccccc) forState:UIControlStateNormal];
  142. button.layer.borderColor = [UIColor clearColor].CGColor;
  143. }else{
  144. [button setBackgroundColor:[UIColor whiteColor]];
  145. UIColor *titleColor = selected ? UIColorFromRGB(0xf85a5a) : UIColorFromRGB(0x666666);
  146. button.layer.borderColor = selected ? [UIColorFromRGB(0xff6362) CGColor] : [UIColorFromRGB(0xcccccc) CGColor];
  147. [button setTitleColor:titleColor forState:UIControlStateNormal];
  148. }
  149. button.selected = selected;
  150. }
  151. - (CGSize)sizeForText:(NSString *)text withFontSize:(CGFloat)fontSize
  152. {
  153. if (text.length == 0 || fontSize <= 0) {
  154. return CGSizeZero;
  155. }
  156. NSDictionary *dict = @{NSFontAttributeName : [UIFont systemFontOfSize:fontSize]};
  157. return [text sizeWithAttributes:dict];
  158. }
  159. // 根据规格按钮算出所在行数
  160. + (NSUInteger)rowNumberForSepcsArray:(NSArray *)specArray index:(NSUInteger)index
  161. {
  162. if (specArray.count == 0 || index >= specArray.count) {
  163. return 0;
  164. }
  165. CGFloat screenMargin = 15; // 规格按钮距离屏幕边距
  166. CGFloat betMargin = 10; // 规格按钮之间边距
  167. CGFloat rowLength = 2 * screenMargin; // 累计行长度
  168. CGFloat rowCount = 0; // 共几行
  169. for (int i = 0; i < index + 1; i++) {
  170. FKBasicSpecItem *basicItem = specArray[i];
  171. if (basicItem.name.length == 0) continue;
  172. CGFloat titleWidth = [FLStringHelper rectOfString:basicItem.name
  173. font:[UIFont systemFontOfSize:kProductSpecBtnFont]
  174. height:CGFLOAT_MAX].size.width;
  175. CGFloat buttonWidth = titleWidth + 2 * KProductSpecBtnTitleMargin;
  176. rowLength += (buttonWidth + (i ? betMargin : 0));
  177. if (rowLength >= UISCREENWIDTH) {
  178. rowCount++;
  179. rowLength = buttonWidth + 2 * screenMargin;
  180. }
  181. }
  182. return rowCount;
  183. }
  184. - (void)specsBtnClick:(UIButton *)sender{
  185. BOOL selected = !sender.selected;
  186. [self clearAllBtnState];
  187. sender.selected = selected;
  188. NSUInteger index = sender.tag - kProductSpecBtnTag;
  189. [self.specItem clearAllSelectState];
  190. FKBasicSpecItem *basicItem = [self basicSpecItemAtIndex:index];
  191. basicItem.selected = sender.selected;
  192. self.specItem.selectedPicUrl = nil;
  193. if (sender.selected && [basicItem.picUrl isKindOfClass:[NSString class]]) self.specItem.selectedPicUrl = basicItem.picUrl;
  194. if ([self.delegate respondsToSelector:@selector(unitView:basicSpecIndex:selected:)]) {
  195. [self.delegate unitView:self basicSpecIndex:index selected:sender.selected];
  196. }
  197. }
  198. - (void)clickSizeBtn:(UIButton *)sender{
  199. if ([self.delegate respondsToSelector:@selector(showSizePic)]){
  200. [self.delegate showSizePic];
  201. }
  202. }
  203. - (void)clickSizeLinkBtn:(UIButton *)sender {
  204. if ([self.delegate respondsToSelector:@selector(showSizeLink)]){
  205. [self.delegate showSizeLink];
  206. }
  207. }
  208. - (FKBasicSpecItem *)basicSpecItemAtIndex:(NSUInteger)index{
  209. if (index < self.specItem.basicSpecArray.count) {
  210. return self.specItem.basicSpecArray[index];
  211. }
  212. return nil;
  213. }
  214. - (void)setShowSizeIcon:(BOOL)showSizeIcon{
  215. self.sizeBtn.hidden = showSizeIcon ? NO : YES;
  216. self.sizeBottonLine.hidden = showSizeIcon ? NO : YES;
  217. self.sizeBtn.userInteractionEnabled = showSizeIcon ? YES : NO;
  218. self.sizeLinkButton.hidden = showSizeIcon ? NO : YES;
  219. }
  220. #pragma mark - property
  221. - (UILabel *)titleLabel{
  222. if (_titleLabel == nil) {
  223. _titleLabel = [[UILabel alloc]init];
  224. _titleLabel.textColor = UIColorFromRGB(0x666666);
  225. _titleLabel.font = [UIFont systemFontOfSize:kProductSpecBtnFont + 1];
  226. [_titleLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
  227. }
  228. return _titleLabel;
  229. }
  230. - (UIButton *)sizeBtn{
  231. if (_sizeBtn == nil) {
  232. _sizeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  233. _sizeBtn.titleLabel.font = [UIFont systemFontOfSize:13];
  234. [_sizeBtn setTitle:@"尺码对照表>" forState:UIControlStateNormal];
  235. [_sizeBtn setTitleColor:UIColorFromRGB(0x999999) forState:UIControlStateNormal];
  236. [_sizeBtn addTarget:self action:@selector(clickSizeBtn:) forControlEvents:UIControlEventTouchUpInside];
  237. }
  238. return _sizeBtn;
  239. }
  240. - (UIButton *)sizeLinkButton {
  241. if (!_sizeLinkButton) {
  242. _sizeLinkButton = [UIButton buttonWithType:UIButtonTypeCustom];
  243. [_sizeLinkButton setImage:[UIImage imageNamed:@"sizeChoiceLinkIcon"] forState:UIControlStateNormal];
  244. [_sizeLinkButton addTarget:self action:@selector(clickSizeLinkBtn:) forControlEvents:UIControlEventTouchUpInside];
  245. }
  246. return _sizeLinkButton;
  247. }
  248. - (UIView *)sizeBottonLine{
  249. if (_sizeBottonLine == nil) {
  250. _sizeBottonLine = [[UIView alloc]init];
  251. _sizeBottonLine.backgroundColor = UIColorFromRGB(0xc5c5c5);
  252. }
  253. return _sizeBottonLine;
  254. }
  255. @end