// // KBTopImageButton.m // keyBoard // // 上图下文按钮:图片在上、文字在下,内部简单布局,适合图标型功能入口 // #import "KBTopImageButton.h" #import "UIColor+Extension.h" @interface KBTopImageButton() @property (nonatomic, strong) UIImageView *iconView; @property (nonatomic, strong) UILabel *textLabel; @end @implementation KBTopImageButton - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { _spacing = 6; _iconSize = CGSizeMake(44, 44); _iconView = [[UIImageView alloc] init]; _iconView.contentMode = UIViewContentModeScaleAspectFit; [self addSubview:_iconView]; _textLabel = [[UILabel alloc] init]; _textLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightSemibold]; _textLabel.textColor = [UIColor colorWithHex:0x2B2B2B]; _textLabel.textAlignment = NSTextAlignmentCenter; _textLabel.numberOfLines = 2; [self addSubview:_textLabel]; } return self; } - (CGSize)intrinsicContentSize { CGFloat width = MAX(_iconSize.width, 60); return CGSizeMake(width, _iconSize.height + _spacing + 34); } - (void)layoutSubviews { [super layoutSubviews]; CGFloat W = CGRectGetWidth(self.bounds); CGFloat H = CGRectGetHeight(self.bounds); CGSize iconS = self.iconSize; CGFloat ix = (W - iconS.width) * 0.5; self.iconView.frame = CGRectMake(ix, 0, iconS.width, iconS.height); CGFloat labelTop = CGRectGetMaxY(self.iconView.frame) + self.spacing; CGFloat lh = MAX(0, H - labelTop); self.textLabel.frame = CGRectMake(0, labelTop, W, lh); } - (void)setHighlighted:(BOOL)highlighted { [super setHighlighted:highlighted]; self.alpha = highlighted ? 0.6 : 1.0; } @end