123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- //
- // FKProductSingleSpecView.m
- // FirstLink
- //
- // Created by jack on 16/1/15.
- // Copyright © 2016年 FirstLink. All rights reserved.
- //
- #import "FKSelectSpecUnitView.h"
- //#import "PindanSpecItem.h"
- #import "FKBasicSpecItem.h"
- #define kProductSpecBtnTag 300
- #define kProductSpecBtnFont 13
- #define KProductSpecBtnTitleMargin 8
- #define kProductSpecBtnHight 27.0f
- @interface FKSelectSpecUnitView ()
- @property (nonatomic, strong) UILabel *titleLabel;
- @property (nonatomic, strong) UIButton *sizeBtn;
- @property (nonatomic, strong) UIView *sizeBottonLine;
- @property (nonatomic, strong) UIButton *sizeLinkButton;
- @end
- @implementation FKSelectSpecUnitView
- - (instancetype)initWithFrame:(CGRect)frame{
- if (self = [super initWithFrame:frame]) {
- [self addAllSubviews];
- }
- return self;
- }
- - (void)addAllSubviews{
-
- [self addSubview:self.titleLabel];
- [self addSubview:self.sizeBtn];
- [self addSubview:self.sizeBottonLine];
-
- [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self).offset(15);
- make.left.equalTo(self).offset(15);
- }];
-
- [self.sizeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
- make.right.equalTo(self).offset(-60);
- make.centerY.equalTo(self.titleLabel);
- make.height.mas_equalTo(25);
- }];
-
- [self.sizeBottonLine mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.right.equalTo(self.sizeBtn);
- make.centerY.equalTo(self.sizeBtn).offset(10);
- make.centerX.equalTo(self.sizeBtn);
- make.height.mas_equalTo(0.5);
- }];
-
- [self addSubview:self.sizeLinkButton];
- [self.sizeLinkButton mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.sizeBtn.mas_right).offset(6);
- make.right.equalTo(self);
- make.centerY.equalTo(self.sizeBtn);
- make.height.mas_equalTo(40);
- }];
-
- self.sizeBtn.hidden = YES;
- self.sizeBottonLine.hidden = YES;
- self.sizeBtn.userInteractionEnabled = NO;
- self.sizeLinkButton.hidden = YES;
- }
- + (CGFloat)contentHeightForSpecItem:(FKProductSpecItem *)specItem{
-
- NSUInteger rowCount = 0;
- if (specItem.basicSpecArray.count){
- rowCount = [self rowNumberForSepcsArray:specItem.basicSpecArray index:specItem.basicSpecArray.count - 1] + 1;
- }
-
- return 15 + 15 + 20 + rowCount * (kProductSpecBtnHight + 10);
- }
- - (void)setContentForSpecItem:(FKProductSpecItem *)specItem{
-
- self.specItem = specItem;
- self.titleLabel.text = specItem.specName;
-
- UIButton *preBtn = nil;
- CGFloat rowNum = 0; // 第几行
-
- for (int i = 0; i < specItem.basicSpecArray.count; i++) {
-
- FKBasicSpecItem *basicItem = specItem.basicSpecArray[i];
-
- BOOL selected = basicItem.selected;
- UIButton *button = [self createSpecBtnWithTitle:basicItem.name
- tag:i + kProductSpecBtnTag
- selected:selected
- canUse:basicItem.canUse];
- [self addSubview:button];
-
- CGFloat btnWidth = [self sizeForText:basicItem.name withFontSize:kProductSpecBtnFont].width + 2 * KProductSpecBtnTitleMargin;
-
- BOOL rowChange = NO; // 判断当前button是否另起一行
- NSUInteger curentRowNum = [FKSelectSpecUnitView rowNumberForSepcsArray:specItem.basicSpecArray index:i];
-
- if (curentRowNum > rowNum && i > 0) {
- rowChange = YES;
- rowNum = curentRowNum;
- }
-
- [button mas_makeConstraints:^(MASConstraintMaker *make) {
-
- if (i == 0){
- make.left.equalTo(self.titleLabel);
- make.top.equalTo(self.titleLabel.mas_bottom).offset(15);
- }else if (rowChange && preBtn){
- make.left.equalTo(self.titleLabel);
- make.top.equalTo(preBtn.mas_bottom).offset(10);
- }else if (preBtn){
- make.left.equalTo(preBtn.mas_right).offset(10);
- make.top.equalTo(preBtn);
- }
-
- make.height.mas_equalTo(kProductSpecBtnHight);
- make.width.mas_equalTo(btnWidth);
- }];
-
- preBtn = button;
- }
-
- }
- - (void)reloadContent{
-
- for (int i = 0; i < self.specItem.basicSpecArray.count; i++) {
- FKBasicSpecItem *basicItem = [self.specItem basicSpecItemAtIndex:i];
- UIButton *button = [self viewWithTag:kProductSpecBtnTag + i];
-
- if ([button isKindOfClass:[UIButton class]]) {
- [self refresButton:button selected:basicItem.selected canUse:basicItem.canUse];
- }
- }
- }
- - (void)clearAllBtnState{
- for (UIButton *button in self.subviews) {
- if ([button isKindOfClass:[UIButton class]]) button.selected = NO;
- }
- }
- - (UIButton *)createSpecBtnWithTitle:(NSString *)title
- tag:(int)tag
- selected:(BOOL)selected
- canUse:(BOOL)canUse
- {
- UIColor *titleColor = selected ? UIColorFromRGB(0xf85a5a) : UIColorFromRGB(0x666666);
-
- UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
- button.tag = tag;
- // button.userInteractionEnabled = canUse;
- button.titleLabel.font = [UIFont systemFontOfSize:kProductSpecBtnFont];
- button.layer.borderWidth = 0.5f;
- button.layer.borderColor = selected ? [UIColorFromRGB(0xff6362) CGColor] : [UIColorFromRGB(0xcccccc) CGColor];
-
- [button addTarget:self action:@selector(specsBtnClick:) forControlEvents:UIControlEventTouchUpInside];
- [button setTitle:title forState:UIControlStateNormal];
- [button setTitleColor:titleColor forState:UIControlStateNormal];
-
- [self refresButton:button selected:selected canUse:canUse];
- return button;
- }
- - (void)refresButton:(UIButton *)button
- selected:(BOOL)selected
- canUse:(BOOL)canUse{
- button.userInteractionEnabled = canUse;
-
- if (!canUse){
- [button setBackgroundColor:UIColorFromRGB(0xeeeeee)];
- [button setTitleColor:UIColorFromRGB(0xcccccc) forState:UIControlStateNormal];
- button.layer.borderColor = [UIColor clearColor].CGColor;
- }else{
- [button setBackgroundColor:[UIColor whiteColor]];
- UIColor *titleColor = selected ? UIColorFromRGB(0xf85a5a) : UIColorFromRGB(0x666666);
- button.layer.borderColor = selected ? [UIColorFromRGB(0xff6362) CGColor] : [UIColorFromRGB(0xcccccc) CGColor];
- [button setTitleColor:titleColor forState:UIControlStateNormal];
- }
-
- button.selected = selected;
- }
- - (CGSize)sizeForText:(NSString *)text withFontSize:(CGFloat)fontSize
- {
- if (text.length == 0 || fontSize <= 0) {
- return CGSizeZero;
- }
- NSDictionary *dict = @{NSFontAttributeName : [UIFont systemFontOfSize:fontSize]};
- return [text sizeWithAttributes:dict];
- }
- // 根据规格按钮算出所在行数
- + (NSUInteger)rowNumberForSepcsArray:(NSArray *)specArray index:(NSUInteger)index
- {
- if (specArray.count == 0 || index >= specArray.count) {
- return 0;
- }
-
- CGFloat screenMargin = 15; // 规格按钮距离屏幕边距
- CGFloat betMargin = 10; // 规格按钮之间边距
- CGFloat rowLength = 2 * screenMargin; // 累计行长度
- CGFloat rowCount = 0; // 共几行
-
- for (int i = 0; i < index + 1; i++) {
-
- FKBasicSpecItem *basicItem = specArray[i];
-
- if (basicItem.name.length == 0) continue;
-
- CGFloat titleWidth = [FLStringHelper rectOfString:basicItem.name
- font:[UIFont systemFontOfSize:kProductSpecBtnFont]
- height:CGFLOAT_MAX].size.width;
-
- CGFloat buttonWidth = titleWidth + 2 * KProductSpecBtnTitleMargin;
-
- rowLength += (buttonWidth + (i ? betMargin : 0));
-
- if (rowLength >= UISCREENWIDTH) {
- rowCount++;
- rowLength = buttonWidth + 2 * screenMargin;
- }
- }
- return rowCount;
- }
- - (void)specsBtnClick:(UIButton *)sender{
-
- BOOL selected = !sender.selected;
- [self clearAllBtnState];
- sender.selected = selected;
-
- NSUInteger index = sender.tag - kProductSpecBtnTag;
- [self.specItem clearAllSelectState];
-
- FKBasicSpecItem *basicItem = [self basicSpecItemAtIndex:index];
- basicItem.selected = sender.selected;
-
- self.specItem.selectedPicUrl = nil;
- if (sender.selected && [basicItem.picUrl isKindOfClass:[NSString class]]) self.specItem.selectedPicUrl = basicItem.picUrl;
-
- if ([self.delegate respondsToSelector:@selector(unitView:basicSpecIndex:selected:)]) {
- [self.delegate unitView:self basicSpecIndex:index selected:sender.selected];
- }
- }
- - (void)clickSizeBtn:(UIButton *)sender{
- if ([self.delegate respondsToSelector:@selector(showSizePic)]){
- [self.delegate showSizePic];
- }
- }
- - (void)clickSizeLinkBtn:(UIButton *)sender {
- if ([self.delegate respondsToSelector:@selector(showSizeLink)]){
- [self.delegate showSizeLink];
- }
- }
- - (FKBasicSpecItem *)basicSpecItemAtIndex:(NSUInteger)index{
- if (index < self.specItem.basicSpecArray.count) {
- return self.specItem.basicSpecArray[index];
- }
- return nil;
- }
- - (void)setShowSizeIcon:(BOOL)showSizeIcon{
-
- self.sizeBtn.hidden = showSizeIcon ? NO : YES;
- self.sizeBottonLine.hidden = showSizeIcon ? NO : YES;
- self.sizeBtn.userInteractionEnabled = showSizeIcon ? YES : NO;
-
- self.sizeLinkButton.hidden = showSizeIcon ? NO : YES;
- }
- #pragma mark - property
- - (UILabel *)titleLabel{
- if (_titleLabel == nil) {
- _titleLabel = [[UILabel alloc]init];
- _titleLabel.textColor = UIColorFromRGB(0x666666);
- _titleLabel.font = [UIFont systemFontOfSize:kProductSpecBtnFont + 1];
- [_titleLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
- }
- return _titleLabel;
- }
- - (UIButton *)sizeBtn{
- if (_sizeBtn == nil) {
- _sizeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- _sizeBtn.titleLabel.font = [UIFont systemFontOfSize:13];
- [_sizeBtn setTitle:@"尺码对照表>" forState:UIControlStateNormal];
- [_sizeBtn setTitleColor:UIColorFromRGB(0x999999) forState:UIControlStateNormal];
-
- [_sizeBtn addTarget:self action:@selector(clickSizeBtn:) forControlEvents:UIControlEventTouchUpInside];
- }
- return _sizeBtn;
- }
- - (UIButton *)sizeLinkButton {
- if (!_sizeLinkButton) {
- _sizeLinkButton = [UIButton buttonWithType:UIButtonTypeCustom];
- [_sizeLinkButton setImage:[UIImage imageNamed:@"sizeChoiceLinkIcon"] forState:UIControlStateNormal];
-
- [_sizeLinkButton addTarget:self action:@selector(clickSizeLinkBtn:) forControlEvents:UIControlEventTouchUpInside];
- }
- return _sizeLinkButton;
- }
- - (UIView *)sizeBottonLine{
- if (_sizeBottonLine == nil) {
- _sizeBottonLine = [[UIView alloc]init];
- _sizeBottonLine.backgroundColor = UIColorFromRGB(0xc5c5c5);
- }
- return _sizeBottonLine;
- }
- @end
|