UIButton 擴(kuò)展胸梆,利用runtime綁定block點(diǎn)擊,圖片文字展示方式须板,擴(kuò)大點(diǎn)擊事件

前言

之前都用的繼承的方式實(shí)現(xiàn)擴(kuò)大點(diǎn)擊事件乳绕,綁定block,最好還是擴(kuò)展為UIButton的分類,特意整理一下逼纸。

效果圖:


button擴(kuò)展.gif

代碼

.h


typedef NS_ENUM(NSUInteger, LXButtonEdgeInsetsStyle) {
    LXButtonEdgeInsetsStyleTop, // image在上洋措,label在下
    LXButtonEdgeInsetsStyleLeft, // image在左,label在右
    LXButtonEdgeInsetsStyleBottom, // image在下杰刽,label在上
    LXButtonEdgeInsetsStyleRight // image在右菠发,label在左
};


#define  Font(f) [UIFont systemFontOfSize:(f)]

typedef void (^ButtonBlock)(UIButton *button);
@interface UIButton (LXExpandBtn)

@property(nonatomic,strong)NSString *buttonId;//標(biāo)識(shí)
@property(nonatomic,copy)ButtonBlock block;//添加點(diǎn)擊事件
@property (nonatomic,assign) UIEdgeInsets hitTestEdgeInsets;//點(diǎn)擊區(qū)域王滤,默認(rèn)為(0,0滓鸠,0雁乡,0); 負(fù)的為擴(kuò)大


//frame初始化
+(UIButton *)LXButtonWithTitle:(NSString *)title  titleFont:(UIFont *)titleLabelFont Image:(UIImage *)image backgroundImage:(UIImage *)backgroundImage backgroundColor:(UIColor *)backgroundColor titleColor:(UIColor *)titleLabelColor frame:(CGRect)frame;
//約束初始化
+(UIButton *)LXButtonNoFrameWithTitle:(NSString *)title  titleFont:(UIFont *)titleLabelFont Image:(UIImage *)image backgroundImage:(UIImage *)backgroundImage backgroundColor:(UIColor *)backgroundColor titleColor:(UIColor *)titleLabelColor;

//添加block
-(void)addClickBlock:(ButtonBlock)block;


/**
 *  設(shè)置button的titleLabel和imageView的布局樣式,及間距
 *
 *  @param style titleLabel和imageView的布局樣式
 *  @param space titleLabel和imageView的間距
 */
- (void)layoutButtonWithEdgeInsetsStyle:(LXButtonEdgeInsetsStyle)style
                        imageTitleSpace:(CGFloat)space;

.m

#import "UIButton+LXExpandBtn.h"
#import <objc/runtime.h>

static const NSString *KEY_ButtonId = @"buttonId";

static const NSString *KEY_ButtonBlock = @"buttonBlock";


static const NSString *KEY_HitTestEdgeInsets = @"hitTestEdgeInsets";

@implementation UIButton (LXExpandBtn)


//擴(kuò)大點(diǎn)擊區(qū)域
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    if(UIEdgeInsetsEqualToEdgeInsets(self.hitTestEdgeInsets, UIEdgeInsetsZero) || !self.enabled || self.hidden)
    {
        return [super pointInside:point withEvent:event];
    }
    
    CGRect relativeFrame = self.bounds;
    CGRect hitFrame = UIEdgeInsetsInsetRect(relativeFrame, self.hitTestEdgeInsets);
    
    return CGRectContainsPoint(hitFrame, point);
}


+(UIButton *)LXButtonWithTitle:(NSString *)title titleFont:(UIFont *)titleLabelFont Image:(UIImage *)image backgroundImage:(UIImage *)backgroundImage backgroundColor:(UIColor *)backgroundColor titleColor:(UIColor *)titleLabelColor frame:(CGRect)frame
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:image forState:UIControlStateNormal];
    [button setBackgroundImage:backgroundImage forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:titleLabelColor forState:UIControlStateNormal];
    button.backgroundColor = backgroundColor;
    button.frame = frame;
    button.titleLabel.font = titleLabelFont;
    
    
    return button;
}
+(UIButton *)LXButtonNoFrameWithTitle:(NSString *)title titleFont:(UIFont *)titleLabelFont Image:(UIImage *)image backgroundImage:(UIImage *)backgroundImage backgroundColor:(UIColor *)backgroundColor titleColor:(UIColor *)titleLabelColor
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:image forState:UIControlStateNormal];
    [button setBackgroundImage:backgroundImage forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:titleLabelColor forState:UIControlStateNormal];
    button.backgroundColor = backgroundColor;
    button.titleLabel.font = titleLabelFont;
    return button;
}
//添加點(diǎn)擊事件-
-(void)addClickBlock:(ButtonBlock)block
{
    
    self.block = block;
    [self addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)buttonAction:(UIButton *)button
{
    self.block(button);
}

- (void)layoutButtonWithEdgeInsetsStyle:(LXButtonEdgeInsetsStyle)style
                        imageTitleSpace:(CGFloat)space
{
    //    self.backgroundColor = [UIColor cyanColor];
    
    /**
     *  前置知識(shí)點(diǎn):titleEdgeInsets是title相對(duì)于其上下左右的inset糜俗,跟tableView的contentInset是類似的踱稍,
     *  如果只有title,那它上下左右都是相對(duì)于button的悠抹,image也是一樣珠月;
     *  如果同時(shí)有image和label,那這時(shí)候image的上左下是相對(duì)于button楔敌,右邊是相對(duì)于label的啤挎;title的上右下是相對(duì)于button,左邊是相對(duì)于image的卵凑。
     */
    
    
    // 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,用下面的這種設(shè)置
        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. 根據(jù)style和space得到imageEdgeInsets和labelEdgeInsets的值
    switch (style) {
        case LXButtonEdgeInsetsStyleTop:
        {
            imageEdgeInsets = UIEdgeInsetsMake(-labelHeight-space/2.0, 0, 0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith, -imageHeight-space/2.0, 0);
        }
            break;
        case LXButtonEdgeInsetsStyleLeft:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, -space/2.0, 0, space/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, space/2.0, 0, -space/2.0);
        }
            break;
        case LXButtonEdgeInsetsStyleBottom:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, 0, -labelHeight-space/2.0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(-imageHeight-space/2.0, -imageWith, 0, 0);
        }
            break;
        case LXButtonEdgeInsetsStyleRight:
        {
            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;
}
#pragma mark--- getter setter--
//分類中不能直接使用setter和getter勺卢、需要使用運(yùn)行時(shí)
- (void)setHitTestEdgeInsets:(UIEdgeInsets)hitTestEdgeInsets
{
    NSValue *value = [NSValue value:&hitTestEdgeInsets withObjCType:@encode(UIEdgeInsets)];
    objc_setAssociatedObject(self, &KEY_HitTestEdgeInsets, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIEdgeInsets)hitTestEdgeInsets
{
    NSValue *value = objc_getAssociatedObject(self, &KEY_HitTestEdgeInsets);
    if(value)
    {
        UIEdgeInsets edgeInsets;
        [value getValue:&edgeInsets];
        return edgeInsets;
    }
    else
    {
        return UIEdgeInsetsZero;
    }
}
-(void)setButtonId:(NSString *)buttonId{
    objc_setAssociatedObject(self, &KEY_ButtonId, buttonId, OBJC_ASSOCIATION_RETAIN);
}
-(NSString *)buttonId{
    return objc_getAssociatedObject(self, &KEY_ButtonId);
    
}
-(void)setBlock:(ButtonBlock)block{
    objc_setAssociatedObject(self, &KEY_ButtonBlock, block, OBJC_ASSOCIATION_RETAIN);
}
-(ButtonBlock)block{
    return objc_getAssociatedObject(self, &KEY_ButtonBlock);
}
@end

使用

 //擴(kuò)大點(diǎn)擊區(qū)域
    UIButton *button2 =[UIButton LXButtonWithTitle:@"button2" titleFont:Font(15) Image:nil backgroundImage:nil backgroundColor:[UIColor whiteColor] titleColor:[UIColor blackColor] frame:CGRectMake(0, 0, 200, 200)];
    
    [button2 setTitleColor:[UIColor purpleColor] forState:UIControlStateHighlighted];

    [button2 setTitle:@"button2點(diǎn)到了" forState:UIControlStateHighlighted];
    button2.layer.borderColor =[UIColor redColor].CGColor;
    button2.layer.borderWidth = 5;
    button2.center = CGPointMake(125, 125);
    
    button2.hitTestEdgeInsets =UIEdgeInsetsMake(-50, -50, -50, -50);
    [view addSubview:button2];
    
    [button2 addClickBlock:^(UIButton *button) {
        NSLog(@"點(diǎn)擊了button2");
    }];
    
    
    //布局方式
    
    [self.topBtn layoutButtonWithEdgeInsetsStyle:LXButtonEdgeInsetsStyleTop imageTitleSpace:10];

demo 地址:UIButton擴(kuò)展

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末琼蚯,一起剝皮案震驚了整個(gè)濱河市左胞,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖纷宇,帶你破解...
    沈念sama閱讀 216,651評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件吃警,死亡現(xiàn)場(chǎng)離奇詭異净赴,居然都是意外死亡哩陕,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門危虱,熙熙樓的掌柜王于貴愁眉苦臉地迎上來羊娃,“玉大人,你說我怎么就攤上這事埃跷∪镧瑁” “怎么了?”我有些...
    開封第一講書人閱讀 162,931評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵弥雹,是天一觀的道長垃帅。 經(jīng)常有香客問我,道長剪勿,這世上最難降的妖魔是什么贸诚? 我笑而不...
    開封第一講書人閱讀 58,218評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上酱固,老公的妹妹穿的比我還像新娘械念。我一直安慰自己,他們只是感情好运悲,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,234評(píng)論 6 388
  • 文/花漫 我一把揭開白布龄减。 她就那樣靜靜地躺著,像睡著了一般班眯。 火紅的嫁衣襯著肌膚如雪希停。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,198評(píng)論 1 299
  • 那天署隘,我揣著相機(jī)與錄音宠能,去河邊找鬼。 笑死定踱,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的恃鞋。 我是一名探鬼主播崖媚,決...
    沈念sama閱讀 40,084評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼恤浪!你這毒婦竟也來了畅哑?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,926評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤水由,失蹤者是張志新(化名)和其女友劉穎荠呐,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體砂客,經(jīng)...
    沈念sama閱讀 45,341評(píng)論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡泥张,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,563評(píng)論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了鞠值。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片媚创。...
    茶點(diǎn)故事閱讀 39,731評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖彤恶,靈堂內(nèi)的尸體忽然破棺而出钞钙,到底是詐尸還是另有隱情,我是刑警寧澤声离,帶...
    沈念sama閱讀 35,430評(píng)論 5 343
  • 正文 年R本政府宣布芒炼,位于F島的核電站,受9級(jí)特大地震影響术徊,放射性物質(zhì)發(fā)生泄漏本刽。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,036評(píng)論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望盅安。 院中可真熱鬧唤锉,春花似錦、人聲如沸别瞭。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蝙寨。三九已至晒衩,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間墙歪,已是汗流浹背听系。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評(píng)論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留虹菲,地道東北人靠胜。 一個(gè)月前我還...
    沈念sama閱讀 47,743評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像毕源,于是被迫代替她去往敵國和親浪漠。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,629評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫霎褐、插件址愿、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,094評(píng)論 4 62
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,079評(píng)論 25 707
  • 現(xiàn)在是四月九日晚九點(diǎn)四十分,我開始第一次嘗試在簡書上寫一些零碎的生活經(jīng)歷冻璃。那么你在干什么呢响谓?我耳邊此時(shí)正播放著B...
    嫭七七閱讀 369評(píng)論 0 0
  • 寫的上一首尋你 已經(jīng)杳無音訊 距離這一首 已經(jīng)浪費(fèi)一千八百多個(gè)日子 之前我愛你的秘密 是我豢養(yǎng)的蝴蝶 和為...
    紅骰子閱讀 281評(píng)論 2 14