前序
在實(shí)際開(kāi)發(fā)中,經(jīng)常會(huì)遇到UIImage+UILabel+點(diǎn)擊事件的需求,比較挫的做法就是用UIButton+UIImage+UILabel三個(gè)控件組合使用。高級(jí)的用法就是使用UIButton一個(gè)控件,設(shè)置UIButton的Image和TitleLabel。
UIButton同時(shí)設(shè)置Image和Label時(shí),默認(rèn)情況是Image在左,Label在右。我們實(shí)際需要的效果是Image在上,Label在下,那么此時(shí)就需要了解并運(yùn)用imageEdgeInsets和titleEdgeInsets。
API理解
關(guān)于imageEdgeInsets和titleEdgeInsets的詳解網(wǎng)上已經(jīng)有很多的文章:
iOS UIButton之UIEdgeInsets詳解
封裝與運(yùn)用
關(guān)于imageEdgeInsets和titleEdgeInsets網(wǎng)絡(luò)上有很多的例子,每次要用到的時(shí)候都要去進(jìn)行一番查詢(比較笨,老是記不住),所以自己進(jìn)行了簡(jiǎn)單的封裝。
擴(kuò)展UIButton
通過(guò)擴(kuò)展UIButton,提供一個(gè)實(shí)例方法,直接設(shè)置Image的方向,就可自動(dòng)設(shè)置出對(duì)應(yīng)的imageEdgeInsets和titleEdgeInsets。
例子
[self.testBtn setImageAndTitleDirection:FlexDirectionTypeImgRight offset:0];
FlexDirectionTypeImgTop

FlexDirectionTypeImgLeft

FlexDirectionTypeImgBottom

FlexDirectionTypeImgRight

代碼地址
此為測(cè)試Demo,如果您想使用該封裝文件,可將Demo中UIButton+flexDirection(.h、.m)文件copy至您的工程。
ButtonFlexDirection