// // FKSelectSpecAmountView.m // FirstLink // // Created by jack on 16/1/18. // Copyright © 2016年 FirstLink. All rights reserved. // #import "FKSelectSpecAmountView.h" @interface FKSelectSpecAmountView () @property (nonatomic, strong) UIButton *addBtn; @property (nonatomic, strong) UIButton *reductBtn; @property (nonatomic, strong) UILabel *numberLabel; @end @implementation FKSelectSpecAmountView - (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { [self addAllSubviews]; } return self; } - (void)configWithSpecDataCenter:(FKProductSpecDataCenter *)dataCenter{ NSUInteger stockCount = [dataCenter getTotalStock]; FKProductItem *targetItem = [dataCenter getSelectedProductItem]; if (targetItem) { stockCount = [targetItem realStockCount]; } if (stockCount <= 10){ self.remainLabel.hidden = NO; self.remainLabel.text = [NSString stringWithFormat:@"(剩余%lu件)", (unsigned long)stockCount]; if (stockCount <= 0) self.remainLabel.text = @"缺货"; } else{ self.remainLabel.hidden = YES; } } #pragma mark - Layout - (void)addAllSubviews{ [self addSubview:self.numberLabel]; [self addSubview:self.addBtn]; [self addSubview:self.reductBtn]; [self addSubview:self.amountLabel]; [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self).offset(15); make.centerY.equalTo(self); }]; [self.addBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self).offset(- 15); make.centerY.equalTo(self); make.width.height.mas_equalTo(27); }]; [self.amountLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.addBtn.mas_left).offset(- 2); make.top.height.equalTo(self.addBtn); make.width.equalTo(@60); }]; [self.reductBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.width.height.equalTo(self.addBtn); make.right.equalTo(self.amountLabel.mas_left).offset(- 2); }]; [self addSubview:self.remainLabel]; [self.remainLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.reductBtn.mas_left).offset(-10); make.centerY.equalTo(self.reductBtn); }]; } #pragma mark - propety - (void)clickAddBtn:(UIButton *)sender{ NSInteger count = self.amountLabel.text.integerValue + 1; self.amountLabel.text = [NSString stringWithFormat:@"%ld", (long)count]; } - (void)clickReduceBtn:(UIButton *)sender{ NSInteger count = self.amountLabel.text.integerValue - 1; if (count <= 0) count = 1; self.amountLabel.text = [NSString stringWithFormat:@"%ld", (long)count]; } #pragma mark - property - (UILabel *)amountLabel{ if (_amountLabel == nil) { _amountLabel = [[UILabel alloc]init]; _amountLabel.layer.borderWidth = 0.5f; _amountLabel.layer.borderColor = [UIColorFromRGB(0xcccccc) CGColor]; _amountLabel.textAlignment = NSTextAlignmentCenter; _amountLabel.lineBreakMode = NSLineBreakByTruncatingTail; _amountLabel.numberOfLines = 1; _amountLabel.textColor = UIColorFromRGB(0x333333); _amountLabel.text = @"1"; } return _amountLabel; } - (UILabel *)remainLabel { if (_remainLabel == nil) { _remainLabel = [[UILabel alloc] init]; _remainLabel.font = [UIFont systemFontOfSize:12]; _remainLabel.textAlignment = NSTextAlignmentCenter; _remainLabel.textColor = UIColorFromRGB(0xff6362); _remainLabel.hidden = YES; } return _remainLabel; } - (UIButton *)addBtn{ if (_addBtn == nil) { _addBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_addBtn setImage:[UIImage imageNamed:@"specification_body_add_n"] forState:UIControlStateNormal]; [_addBtn addTarget:self action:@selector(clickAddBtn:) forControlEvents:UIControlEventTouchUpInside]; } return _addBtn; } - (UIButton *)reductBtn{ if (_reductBtn == nil) { _reductBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_reductBtn setImage:[UIImage imageNamed:@"specification_body_lower_n"] forState:UIControlStateNormal]; [_reductBtn addTarget:self action:@selector(clickReduceBtn:) forControlEvents:UIControlEventTouchUpInside]; } return _reductBtn; } - (UILabel *)numberLabel{ if (_numberLabel == nil) { _numberLabel = [[UILabel alloc]init]; _numberLabel.backgroundColor = [UIColor clearColor]; _numberLabel.font = [UIFont systemFontOfSize:15]; _numberLabel.textColor = UIColorFromRGB(0x333333); _numberLabel.text = @"数量"; } return _numberLabel; } @end