titleEdgeInsets和imageEdgeInsets 调整Button内部布局
要实现上述功能, 我们可以给UIButton来写一个分类:
typedef NS_ENUM(NSUInteger, AxsButtonEdgeInsetsStyle) {
GYButtonEdgeInsetsStyleTop,
GYButtonEdgeInsetsStyleLeft,
GYButtonEdgeInsetsStyleBottom,
GYButtonEdgeInsetsStyleRight,
GYButtonEdgeInsetsStyleRightBottom,
GYButtonEdgeInsetsStyleDefault,
};
@interface UIButton (ImageTitleSpacing)
- (void)layoutButtonWithEdgeInsetsStyle:(GYButtonEdgeInsetsStyle)style
imageTitleSpace:(CGFloat)space;
- (void)layoutButtonWithEdgeInsetsStyle:(GYButtonEdgeInsetsStyle)style
imageTitleSpace:(CGFloat)space
isAdaption:(BOOL)isAdaption;
- (void)layoutButtonWithEdgeInsetsStyle:(GYButtonEdgeInsetsStyle)style
imageTitleSpace:(CGFloat)space
{
[self layoutButtonWithEdgeInsetsStyle:style imageTitleSpace:space isAdaption:NO];
}
- (void)layoutButtonWithEdgeInsetsStyle:(GYButtonEdgeInsetsStyle)style
imageTitleSpace:(CGFloat)space
isAdaption:(BOOL)isAdaption{
CGFloat imageWith = CGRectGetWidth(self.imageView.frame);
CGFloat imageHeight = CGRectGetHeight(self.imageView.frame);
CGFloat labelWidth = 0.0;
CGFloat labelHeight = 0.0;
if (isAdaption) {
labelWidth = self.titleLabel.frame.size.width;
labelHeight = self.titleLabel.frame.size.height;
} else {
labelWidth = self.titleLabel.intrinsicContentSize.width;
labelHeight = self.titleLabel.intrinsicContentSize.height;
}
UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
UIEdgeInsets labelEdgeInsets = UIEdgeInsetsZero;
switch (style) {
case GYButtonEdgeInsetsStyleTop:
{
imageEdgeInsets = UIEdgeInsetsMake(-labelHeight-space/2.0, 0, 0, -labelWidth);
labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith, -imageHeight-space/2.0, 0);
}
break;
case GYButtonEdgeInsetsStyleLeft:
{
imageEdgeInsets = UIEdgeInsetsMake(0, -space/2.0, 0, space/2.0);
labelEdgeInsets = UIEdgeInsetsMake(0, space/2.0, 0, -space/2.0);
}
break;
case GYButtonEdgeInsetsStyleBottom:
{
imageEdgeInsets = UIEdgeInsetsMake(0, 0, -labelHeight-space/2.0, -labelWidth);
labelEdgeInsets = UIEdgeInsetsMake(-imageHeight-space/2.0, -imageWith, 0, 0);
}
break;
case GYButtonEdgeInsetsStyleRight:
{
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;
case GYButtonEdgeInsetsStyleRightBottom:
{
imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth+space/2.0, -labelHeight/2.0+imageHeight/2.0, -labelWidth-space/2.0);
labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith-space/2.0, 0, imageWith+space/2.0);
break;
}
case GYButtonEdgeInsetsStyleDefault:
{
imageEdgeInsets = UIEdgeInsetsZero;
labelEdgeInsets = UIEdgeInsetsZero;
}
break;
default:
break;
}
self.titleEdgeInsets = labelEdgeInsets;
self.imageEdgeInsets = imageEdgeInsets;
}
使用:
AxsEnlargeButton *button = [self newButtonWithImage:nil
highlightedImage:nil
frame:frame
title:title
titleColor:titleColor
titleShadowColor:nil
font:kFontSize6_new(14)
target:self
action:@selector(bottomItemClick:)];
[button setImage:icon forState:UIControlStateNormal];
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[button layoutButtonWithEdgeInsetsStyle:style imageTitleSpace:scaleX_6(3)];
扩展UIbutton的点击区域
@interface AxsEnlargeButton : UIButton
@property (nonatomic, assign) CGFloat enlargedEdge;
@property (nonatomic, assign) UIEdgeInsets enlargedEdgeInsets;
- (void)setEnlargedEdgeWithTop:(CGFloat)top
left:(CGFloat)left
bottom:(CGFloat)bottom
right:(CGFloat)right;
.m文件内容:
- (void)setEnlargedEdge:(CGFloat)enlargedEdge {
_enlargedEdge = enlargedEdge;
self.enlargedEdgeInsets = UIEdgeInsetsMake(enlargedEdge, enlargedEdge, enlargedEdge, enlargedEdge);
}
- (void)setEnlargedEdgeWithTop:(CGFloat)top left:(CGFloat)left bottom:(CGFloat)bottom right:(CGFloat)right {
self.enlargedEdgeInsets = UIEdgeInsetsMake(top, left, bottom, right);
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
if(self.alpha <= 0.01 || !self.userInteractionEnabled || self.hidden){
return nil;
}
UIEdgeInsets enlarge = self.enlargedEdgeInsets;
if (UIEdgeInsetsEqualToEdgeInsets(enlarge, UIEdgeInsetsZero)) {
return [super hitTest:point withEvent:event];
}
CGRect enlargedRect = [self enlargeRect:self.bounds insets:enlarge];
return [self testContainsPoint:enlargedRect point:point] ? self :nil;
}
- (BOOL)testContainsPoint:(CGRect)rect point:(CGPoint)point {
UIWindow *window = [UIApplication sharedApplication].keyWindow;
CGRect rectInWindow = [self convertRect:rect toView:window];
CGPoint pointInWindow = [self convertPoint:point toView:window];
return CGRectContainsPoint(rectInWindow, pointInWindow);
}
- (CGRect)enlargeRect:(CGRect)rect insets:(UIEdgeInsets)enlarge {
CGRect enlargeRect = CGRectMake(rect.origin.x - enlarge.left, rect.origin.y - enlarge.top, rect.size.width + enlarge.left + enlarge.right, rect.size.height + enlarge.top + enlarge.bottom);
return enlargeRect;
}
|