#import <UIKit/UIKit.h>
typedef void (^TouchedBlock)(UIButton *btn);
typedef NS_ENUM(NSUInteger, SSButtonEdgeInsetsStyle) {
SSButtonEdgeInsetsStyleTop, // image在上,label在下
SSButtonEdgeInsetsStyleLeft, // image在左,label在右
SSButtonEdgeInsetsStyleBottom, // image在下,label在上
SSButtonEdgeInsetsStyleRight // image在右,label在左
};
@interface UIButton (SSExtension)
-(void)addTouchUpInsideHandler:(TouchedBlock)handler;
//button不同狀態的背景顏色(代替圖片)
- (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state;
/**
* 設定屬性文字
*
* @param textArr 需要顯示的文字陣列,如果有換行請在文字中添加 "\n"換行符
* @param fontArr 字體陣列, 如果fontArr與textArr個數不相同則獲取字體陣列中最后一個字體
* @param colorArr 顏色陣列, 如果colorArr與textArr個數不相同則獲取字體陣列中最后一個顏色
* @param spacing 換行的行間距
* @param alignment 換行的文字對齊方式
*/
- (void)setAttriStrWithTextArray:(NSArray *)textArr fontArr:(NSArray *)fontArr colorArr:(NSArray *)colorArr lineSpacing:(CGFloat)spacing alignment:(NSTextAlignment)alignment;
/**
* 初始化一個按鈕 沒有邊框的 背景顏色是藍色的 橢圓
*/
+ (UIButton *)buttonTitle:(NSString *)title target:(id)target action:(SEL)action;
/**
* 創建一個button 按鈕size為 25*25 實際圖片大小為17.5*17.5 UI要求
*
*/
+ (UIButton *)buttonMustItemWithImage:(UIImage *)image highImage:(UIImage *)highImage target:(id)target action:(SEL)action;
- (void)setEnlargeEdgeWithTop:(CGFloat)top right:(CGFloat)right bottom:(CGFloat)bottom left:(CGFloat)left;
/**
* 設定button的titleLabel和imageView的布局樣式,及間距
*
* @param style titleLabel和imageView的布局樣式
* @param space titleLabel和imageView的間距
*/
- (void)layoutButtonWithEdgeInsetsStyle:(SSButtonEdgeInsetsStyle)style
imageTitleSpace:(CGFloat)space;
@end
#import "UIButton+SSExtension.h"
#import <objc/runtime.h>
static char topNameKey;
static char rightNameKey;
static char bottomNameKey;
static char leftNameKey;
static const void *UIButtonBlockKey = &UIButtonBlockKey;
@implementation UIButton (SSExtension)
#pragma mark - ============ 給按鈕點擊事件 ============
-(void)addTouchUpInsideHandler:(TouchedBlock)handler
{
objc_setAssociatedObject(self, UIButtonBlockKey, handler, OBJC_ASSOCIATION_COPY_NONATOMIC);
[self addTarget:self action:@selector(cc_touchUpInsideAction:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)cc_touchUpInsideAction:(UIButton *)btn{
TouchedBlock block = objc_getAssociatedObject(self, UIButtonBlockKey);
if (block) {
block(btn);
}
}
#pragma mark - ============ 給按鈕設定狀態顏色 ============
/**
* 設定按鈕背景顏色
*/
- (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state
{
[self setBackgroundImage:[UIButton imageWithColor:backgroundColor] forState:state];
}
- (void)setLayerBorderColor:(UIColor *)borderColor forState:(UIControlState)state
{
[self.layer setBorderColor:borderColor.CGColor];
}
+ (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
#pragma mark - ============ 設定按鈕的屬性文字 ============
/**
* 設定屬性文字
*
* @param textArr 需要顯示的文字陣列,如果有換行請在文字中添加 "\n"換行符
* @param fontArr 字體陣列, 如果fontArr與textArr個數不相同則獲取字體陣列中最后一個字體
* @param colorArr 顏色陣列, 如果colorArr與textArr個數不相同則獲取字體陣列中最后一個顏色
* @param spacing 換行的行間距
* @param alignment 換行的文字對齊方式
*/
- (void)setAttriStrWithTextArray:(NSArray *)textArr fontArr:(NSArray *)fontArr colorArr:(NSArray *)colorArr lineSpacing:(CGFloat)spacing alignment:(NSTextAlignment)alignment
{
if (textArr.count >0 && fontArr.count >0 && colorArr.count > 0) {
NSMutableString *allString = [NSMutableString string];
for (NSString *tempText in textArr) {
[allString appendFormat:@"%@",tempText];
}
NSRange lastTextRange = NSMakeRange(0, 0);
NSMutableArray *rangeArr = [NSMutableArray array];
for (NSString *tempText in textArr) {
NSRange range = [allString rangeOfString:tempText];
//如果存在相同字符,則換一種查找的方法
if ([allString componentsSeparatedByString:tempText].count>2) { //存在多個相同字符
range = NSMakeRange(lastTextRange.location+lastTextRange.length, tempText.length);
}
[rangeArr addObject:NSStringFromRange(range)];
lastTextRange = range;
}
//設定屬性文字
NSMutableAttributedString *textAttr = [[NSMutableAttributedString alloc] initWithString:allString];
for (int i=0; i<textArr.count; i++) {
NSRange range = NSRangeFromString(rangeArr[i]);
UIFont *font = (i > fontArr.count-1) ? [fontArr lastObject] : fontArr[i];
[textAttr addAttribute:NSFontAttributeName value:font range:range];
UIColor *color = (i > colorArr.count-1) ? [colorArr lastObject] : colorArr[i];
[textAttr addAttribute:NSForegroundColorAttributeName value:color range:range];
}
//如果需要換行
if ([allString rangeOfString:@"\n"].location != NSNotFound) {
self.titleLabel.numberOfLines = 0;
}
[self setAttributedTitle:textAttr forState:0];
//段落 <如果有換行 或者 字體寬度超過一行就設定行間距>
if (self.width > kFullScreenWidth || [allString rangeOfString:@"\n"].location != NSNotFound) {
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = spacing;
paragraphStyle.alignment = alignment;
self.titleLabel.numberOfLines = 0;
[textAttr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0,allString.length)];
[self setAttributedTitle:textAttr forState:0];
}
} else {
[self setTitle:@"文字,顏色,字體 每個陣列至少有一個" forState:0];
}
}
/**
* 初始化一個按鈕 沒有邊框的 背景顏色是藍色的 橢圓
*/
+ (UIButton *)buttonTitle:(NSString *)title target:(id)target action:(SEL)action
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:title forState:UIControlStateNormal];
[button setTitleColor:UIColorFromHex(0x666666) forState:UIControlStateNormal];
// button.titleLabel.font = FONTDEFAULT(14);
CGFloat width = [title calculateheight:button.titleLabel.font ].width + 8;
button.frame = CGRectMake(0, 0, width + 16, 30);
if (title.length == 0 ) {
button.backgroundColor = [UIColor clearColor];
}else{
// button.backgroundColor = UIColorFromHex(COLOR_8CC63F);
}
button.layer.cornerRadius = button.height / 2 ;
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
return button;
}
/**
* 創建一個button 按鈕size為 25*25 實際圖片大小為17.5*17.5 UI要求
*
*/
+ (UIButton *)buttonMustItemWithImage:(UIImage *)image highImage:(UIImage *)highImage target:(id)target action:(SEL)action
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:image forState:UIControlStateNormal];
[button setImage:highImage forState:UIControlStateHighlighted];
button.frame = (CGRect){CGPointZero, CGSizeMake(40, 40)};// 圖片
[button setImageEdgeInsets:UIEdgeInsetsMake(0, -20, 0,0)];
[button setEnlargeEdgeWithTop:0 right:40 bottom:0 left:20];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
return button;
}
- (void)setEnlargeEdgeWithTop:(CGFloat)top right:(CGFloat)right bottom:(CGFloat)bottom left:(CGFloat)left {
objc_setAssociatedObject(self, &topNameKey, [NSNumber numberWithFloat:top], OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_setAssociatedObject(self, &rightNameKey, [NSNumber numberWithFloat:right], OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_setAssociatedObject(self, &bottomNameKey, [NSNumber numberWithFloat:bottom], OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_setAssociatedObject(self, &leftNameKey, [NSNumber numberWithFloat: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) {
return CGRectMake(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 = [self enlargedRect];
//如果按鈕設定為不可點擊、隱藏、透明度小于等于0.01或者點擊在按鈕內部,則直接執行父類方法
if (CGRectEqualToRect(rect, self.bounds) || self.userInteractionEnabled == NO || self.hidden == YES || self.alpha <= 0.01) {
return [super hitTest:point withEvent:event];
}
//判斷點擊是否在放大的范圍內
return CGRectContainsPoint(rect, point) ? self : nil;
}
- (void)layoutButtonWithEdgeInsetsStyle:(SSButtonEdgeInsetsStyle)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 ([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 SSButtonEdgeInsetsStyleTop:
{
labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith, -imageHeight-space/2, 0);
imageEdgeInsets = UIEdgeInsetsMake(-labelHeight-space/2, 0, 0, -labelWidth);
}
break;
case SSButtonEdgeInsetsStyleLeft:
{
imageEdgeInsets = UIEdgeInsetsMake(0, -space/2.0, 0, space/2.0);
labelEdgeInsets = UIEdgeInsetsMake(0, space/2.0, 0, -space/2.0);
}
break;
case SSButtonEdgeInsetsStyleBottom:
{
imageEdgeInsets = UIEdgeInsetsMake(0, 0, -labelHeight-space/2.0, -labelWidth);
labelEdgeInsets = UIEdgeInsetsMake(-imageHeight-space/2.0, -imageWith, 0, 0);
}
break;
case SSButtonEdgeInsetsStyleRight:
{
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/210050.html
標籤:java
