// // TDButton.m // TheoryNetwork // // Created by tederen on 2019/9/20. // Copyright © 2019 tederen. All rights reserved. // #import "TDButton.h" @interface TDButton () @property (nonatomic) CGSize hotZoneSize; @property (nonatomic, assign) BOOL hasCustHotZone; @property (nonatomic, strong) UILabel *badgeLabel; @end @implementation TDButton - (instancetype)init { self = [super init]; if (self) { self.adjustsImageWhenHighlighted = NO; } return self; } - (void)setCurrentButtonHotSize:(CGSize)size { self.hasCustHotZone = YES; //热区不应小于44x44 if (size.width < 44) { size.width = 44; } if (size.height < 44) { size.height = 44; } _hotZoneSize = size; } - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event { if (self.hasCustHotZone) { CGRect bounds = self.bounds; CGFloat widthDelta = MAX(self.hotZoneSize.width - bounds.size.width, 0); CGFloat heightDelta = MAX(self.hotZoneSize.height - bounds.size.height, 0); bounds = CGRectInset(bounds, - 0.5 * widthDelta, - 0.5 * heightDelta); return CGRectContainsPoint(bounds, point); } return [super pointInside:point withEvent:event]; } - (void)verticalImageAndTitle:(CGFloat)spacing { CGSize imageSize = self.imageView.frame.size; CGSize titleSize = self.titleLabel.frame.size; CGSize textSize = [self.titleLabel sizeThatFits:CGSizeZero]; CGSize frameSize = CGSizeMake(ceilf(textSize.width), ceilf(textSize.height)); if (titleSize.width + 0.5 < frameSize.width) { titleSize.width = frameSize.width; } CGFloat totalHeight = (imageSize.height + titleSize.height + spacing); self.imageEdgeInsets = UIEdgeInsetsMake(- (totalHeight - imageSize.height), 0.0, 0.0, - titleSize.width); self.titleEdgeInsets = UIEdgeInsetsMake(0, - imageSize.width, - (totalHeight - titleSize.height), 0); } - (void)setButtonAlpha:(CGFloat)alpha { [UIView animateWithDuration:0.2 animations:^{ self.alpha = alpha; }]; } - (void)setButtonBadge:(NSInteger)num isSelect:(BOOL)isSelect{ [self addSubview:self.badgeLabel]; _badgeLabel.textColor = isSelect ? UIColorHex(168BFF): UIColorHex(999999); _badgeLabel.font = [UIFont systemFontOfSize:9.f]; _badgeLabel.text = [NSString stringWithFormat:@"%ld", (long)num]; CGSize badgeSize = [_badgeLabel sizeThatFits:CGSizeZero]; _badgeLabel.frame = CGRectMake(self.frame.size.width-2.5, -4.8, badgeSize.width, badgeSize.height); _badgeLabel.hidden = num == 0 ? YES : NO; } - (void)setButtonBadge:(NSInteger)num { [self addSubview:self.badgeLabel]; _badgeLabel.textColor = UIColorHex(168BFF); _badgeLabel.font = [UIFont systemFontOfSize:9.f]; _badgeLabel.text = [NSString stringWithFormat:@"%ld", (long)num]; CGSize badgeSize = [_badgeLabel sizeThatFits:CGSizeZero]; _badgeLabel.frame = CGRectMake(self.frame.size.width-2.5, -4.8, badgeSize.width, badgeSize.height); _badgeLabel.hidden = num == 0 ? YES : NO; } - (UILabel *)badgeLabel { if (!_badgeLabel) { _badgeLabel = [UILabel new]; } return _badgeLabel; } #pragma mark - otherLayout - (void)layoutButtonWithEdgeInsetsStyle:(GLButtonEdgeInsetsStyle)style imageTitleSpace:(CGFloat)space { // 1. 得到imageView和titleLabel的宽、高 CGFloat imageWith = self.imageView.image.size.width; CGFloat imageHeight = self.imageView.image.size.height; CGFloat labelWidth = 0.0; CGFloat labelHeight = 0.0; if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) { // 由于iOS8中titleLabel的size为0,用下面的这种设置 labelWidth = self.titleLabel.intrinsicContentSize.width; labelHeight = self.titleLabel.intrinsicContentSize.height; } else { labelWidth = self.titleLabel.frame.size.width; labelHeight = self.titleLabel.frame.size.height; } // 2. 声明全局的imageEdgeInsets和labelEdgeInsets UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero; UIEdgeInsets labelEdgeInsets = UIEdgeInsetsZero; // 3. 根据style和space得到imageEdgeInsets和labelEdgeInsets的值 switch (style) { case GLButtonEdgeInsetsStyleTop: { imageEdgeInsets = UIEdgeInsetsMake(-labelHeight-space/2.0, 0, 0, -labelWidth); labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith, -imageHeight-space/2.0, 0); } break; case GLButtonEdgeInsetsStyleLeft: { imageEdgeInsets = UIEdgeInsetsMake(0, -space/2.0, 0, space/2.0); labelEdgeInsets = UIEdgeInsetsMake(0, space/2.0, 0, -space/2.0); } break; case GLButtonEdgeInsetsStyleBottom: { imageEdgeInsets = UIEdgeInsetsMake(0, 0, -labelHeight-space/2.0, -labelWidth); labelEdgeInsets = UIEdgeInsetsMake(-imageHeight-space/2.0, -imageWith, 0, 0); } break; case GLButtonEdgeInsetsStyleRight: { imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth+space/2.0, 0, -labelWidth-space/2.0); labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith-space/2.0, 0, imageWith+space/2.0); } break; default: break; } // 4. 赋值 self.titleEdgeInsets = labelEdgeInsets; self.imageEdgeInsets = imageEdgeInsets; } @end