// // CGXVerticalMenuCollectionCell..m // CGXVerticalMenuView-OC // // Created by CGX on 2018/05/01. // Copyright © 2019 CGX. All rights reserved. // #import "CGXVerticalMenuCollectionCell.h" @interface CGXVerticalMenuCollectionCell() @property (nonatomic , strong) CGXVerticalMenuCollectionItemModel *model; @property (nonatomic , strong) NSLayoutConstraint *hotImageTop; @property (nonatomic , strong) NSLayoutConstraint *hotImageLeft; @property (nonatomic , strong) NSLayoutConstraint *hotImageRight; @property (nonatomic , strong) NSLayoutConstraint *hotImageBottom; @property (nonatomic , strong) NSLayoutConstraint *hotTitleHeight; @property (nonatomic , strong) NSLayoutConstraint *hotTitleleft; @property (nonatomic , strong) NSLayoutConstraint *hotTitleRight; @property (nonatomic , strong) NSLayoutConstraint *hotTitleBottom; @end @implementation CGXVerticalMenuCollectionCell - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self initializeViews]; } return self; } - (instancetype)initWithCoder:(NSCoder *)coder { self = [super initWithCoder:coder]; if (self) { [self initializeViews]; } return self; } - (void)initializeViews { self.layer.masksToBounds = YES; self.clipsToBounds = YES; self.urlImageView = [[UIImageView alloc] init]; self.urlImageView.contentMode = UIViewContentModeScaleAspectFit; [self.contentView addSubview:self.urlImageView]; self.urlImageView.frame = self.bounds; self.urlImageView.layer.masksToBounds = YES; self.urlImageView.clipsToBounds = YES; self.urlImageView.translatesAutoresizingMaskIntoConstraints = NO; self.titleLabel =[[UILabel alloc] init]; self.titleLabel.textColor = [UIColor blackColor]; self.titleLabel.font = [UIFont systemFontOfSize:14]; self.titleLabel.numberOfLines = 1; self.titleLabel.textAlignment = NSTextAlignmentCenter; [self.contentView addSubview:self.titleLabel]; self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO; self.hotImageTop = [NSLayoutConstraint constraintWithItem:self.urlImageView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]; self.hotImageLeft = [NSLayoutConstraint constraintWithItem:self.urlImageView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]; self.hotImageRight = [NSLayoutConstraint constraintWithItem:self.urlImageView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]; self.hotImageBottom = [NSLayoutConstraint constraintWithItem:self.urlImageView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]; [self.contentView addConstraint:self.hotImageTop]; [self.contentView addConstraint:self.hotImageLeft]; [self.contentView addConstraint:self.hotImageRight]; [self.contentView addConstraint:self.hotImageBottom]; self.hotTitleHeight = [NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:30]; [self.titleLabel addConstraint:self.hotTitleHeight]; self.hotTitleleft = [NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]; self.hotTitleRight = [NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]; self.hotTitleBottom = [NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]; [self.contentView addConstraint:self.hotTitleleft]; [self.contentView addConstraint:self.hotTitleRight]; [self.contentView addConstraint:self.hotTitleBottom]; self.titleLabel.hidden = YES; } - (void)layoutSubviews { [super layoutSubviews]; [self.contentView setNeedsLayout]; [self.contentView layoutIfNeeded]; } - (void)reloadData:(CGXVerticalMenuCollectionItemModel *)model { self.model = model; self.contentView.backgroundColor = model.itemBgColor; self.hotImageTop.constant = 0; self.hotImageLeft.constant = 0;; self.hotImageRight.constant = 0; self.hotImageBottom.constant = 0; self.hotTitleHeight.constant = model.itemHeight; self.hotTitleleft.constant = 0; self.hotTitleRight.constant = 0; self.hotTitleBottom.constant = 0; self.titleLabel.hidden = YES; if (model.itemText && model.itemText.length>0) { self.titleLabel.hidden = NO; self.hotImageBottom.constant = -model.itemHeight-model.itemSpace; } self.urlImageView.layer.cornerRadius = model.itemCornerRadius; self.contentView.layer.borderWidth = model.itemBborderWidth; self.contentView.layer.borderColor = [model.itemBorderColor CGColor]; self.contentView.layer.cornerRadius = model.itemCornerRadius; __weak typeof(self) weakSelf = self; if ([model.itemUrlStr hasPrefix:@"http:"] || [model.itemUrlStr hasPrefix:@"https:"]) { if (model.menu_ImageCallback) { model.menu_ImageCallback(weakSelf.urlImageView, [NSURL URLWithString:model.itemUrlStr]); } } else{ if (model.itemUrlStr) { UIImage *image = [UIImage imageNamed:model.itemUrlStr]; if (!image) { image = [UIImage imageWithContentsOfFile:model.itemUrlStr]; self.urlImageView.image = image; } } if (model.menu_ImageCallback) { model.menu_ImageCallback(weakSelf.urlImageView, [NSURL URLWithString:@""]); } } self.titleLabel.text = model.itemText; self.titleLabel.textColor = model.itemColor; self.titleLabel.font = model.itemFont; self.titleLabel.numberOfLines = model.numberOfLines; } @end