ios UIButton 熱區範圍、文字圖片位置等拓展類實現
專案開發中,經常會遇到,按鈕範圍太小,點不到按鈕,導致體驗效果變差的情況.此時,可以給按鈕設定個拓展類,來設定按鈕的點選範圍.解決這一問題!
1.按鈕熱區範圍:
#import <UIKit/UIKit.h>
@interface UIButton (JKEnlargeTouchArea)
/** 設定按鈕的點選範圍
*/
- (void)setEnlargeEdgeWithTop:(CGFloat) top right:(CGFloat) right bottom:(CGFloat) bottom left:(CGFloat) left;
@end
// 設定按鈕的點選範圍
#import "UIButton+JKEnlargeTouchArea.h"
#import <objc/runtime.h>
@implementation UIButton (JKEnlargeTouchArea)
static char topNameKey;
static char rightNameKey;
static char bottomNameKey;
static char leftNameKey;
- (void) setEnlargeEdgeWithTop:(CGFloat) top right:(CGFloat
{
objc_setAssociatedObject(self, &topNameKey, [NSNumbernumberWithFloat:top], OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_setAssociatedObject(self, &rightNameKey, [NSNumbernumberWithFloat:right], OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_setAssociatedObject(
objc_setAssociatedObject(self, &leftNameKey, [NSNumbernumberWithFloat:left], OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (CGRect) enlargedRect
{
NSNumber* topEdge = objc_getAssociatedObject(self, &topNameKey);
NSNumber* rightEdge = objc_getAssociatedObject(self, &rightNameKey);
NSNumber* bottomEdge = objc_getAssociatedObject(self, &bottomNameKey);
NSNumber* leftEdge = objc_getAssociatedObject(self, &leftNameKey);
if (topEdge && rightEdge && bottomEdge && leftEdge)
{
returnCGRectMake(self.bounds.origin.x - leftEdge.floatValue,
self.bounds.origin.y - topEdge.floatValue,
self.bounds.size.width + leftEdge.floatValue + rightEdge.floatValue,
self.bounds.size.height + topEdge.floatValue + bottomEdge.floatValue);
}
else
{
return self.bounds;
}
}
- (UIView*) hitTest:(CGPoint) point withEvent:(UIEvent*) event
{
CGRect rect = [selfenlargedRect];
if (CGRectEqualToRect(rect, self.bounds))
{
return [super hitTest:point withEvent:event];
}
return CGRectContainsPoint(rect, point) ? self : nil;
}
@end
2.按鈕實現圖片文字效果#import <UIKit/UIKit.h>
// 定義一個列舉(包含了四種類型的button)
typedef NS_ENUM(NSUInteger,JKButtonEdgeInsetsStyle) {
JKButtonEdgeInsetsStyleTop,//image在上,label在下
JKButtonEdgeInsetsStyleLeft,//image在左,label在右
JKButtonEdgeInsetsStyleBottom,//image在下,label在上
JKButtonEdgeInsetsStyleRight//image在右,label在左
};
@interface UIButton (JKButtonImageTitleSpacing)
/**
* 設定button的titleLabel和imageView的佈局樣式,及間距
*
* @param style titleLabel和imageView的佈局樣式
* @param space titleLabel和imageView的間距
*/
- (void)layoutButtonWithEdgeInsetsStyle:(JKButtonEdgeInsetsStyle)style imageTitleSpace:(CGFloat)space;
@end
#import "UIButton+JKButtonImageTitleSpacing.h"
@implementation UIButton (JKButtonImageTitleSpacing)
-(void)layoutButtonWithEdgeInsetsStyle:(JKButtonEdgeInsetsStyle)style imageTitleSpace:(CGFloat)space
{
// 1. 得到imageView和titleLabel的寬、高
CGFloat imageWith = self.imageView.frame.size.width;
CGFloat imageHeight = self.imageView.frame.size.height;
CGFloat labelWidth = 0.0;
CGFloat labelHeight = 0.0;
if ([UIDevicecurrentDevice].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) {
caseJKButtonEdgeInsetsStyleTop:
{
imageEdgeInsets = UIEdgeInsetsMake(-labelHeight-space/2.0, 0, 0, -labelWidth);
labelEdgeInsets = UIEdgeInsetsMake(0, -14, -imageHeight-space/2.0, 0)
;
}
break;
caseJKButtonEdgeInsetsStyleLeft:
{
imageEdgeInsets = UIEdgeInsetsMake(0, -space/2.0, 0, space/2.0);
labelEdgeInsets = UIEdgeInsetsMake(0, space/2.0, 0, -space/2.0);
}
break;
caseJKButtonEdgeInsetsStyleBottom:
{
imageEdgeInsets = UIEdgeInsetsMake(0, 0, -labelHeight-space/2.0, -labelWidth);
labelEdgeInsets = UIEdgeInsetsMake(-imageHeight-space/2.0, -imageWith, 0, 0);
}
break;
caseJKButtonEdgeInsetsStyleRight:
{
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