iOS使用帶字體圖標(biāo)的UIButton(支持各種方向)

1.圖標(biāo)字體的導(dǎo)入及使用: https://segmentfault.com/a/1190000004300281
2.我使用的是繼承的方式悬垃,沒有使用原生UIButton的title和imageView,而是自己增加兩個(gè)UILabel到UIButton上去访忿,好處是是按鈕可以高度自定義是牢,布局采用的是Masonry自適應(yīng)。
3.相關(guān)代碼:
ZMButton.h

#import <UIKit/UIKit.h>
/**
 *  默認(rèn)圖標(biāo)在右邊
 */
typedef NS_ENUM(NSInteger,ButtonIconType) {
    ButtonIconTypeNormal = 0,
    ButtonIconTypeLeft,
    ButtonIconTypeTop,
    ButtonIconTypeBottom
};
@interface ZMButton : UIButton

/** 標(biāo)題標(biāo)簽 */
@property (nonatomic, strong) UILabel       *buttonTitleLabel;
/** 字體圖標(biāo)標(biāo)簽 */
@property (nonatomic, strong) UILabel       *buttonIconLabel;
/** 標(biāo)題 */
@property (nonatomic, copy)   NSString      *buttonTitle;
/** 圖標(biāo) */
@property (nonatomic, copy)   NSString      *buttonIcon;
/** 圖標(biāo)類型 */
@property (nonatomic, assign) ButtonIconType iconType;
/** 公共間距 */
@property (nonatomic, assign) CGFloat        margin;
/** 左間距 */
@property (nonatomic, assign) CGFloat        marginLeft;
/** 上間距 */
@property (nonatomic, assign) CGFloat        marginTop;
/** 按鈕總寬度(包含間距) */
@property (nonatomic, assign) CGFloat        totalWidth;
/** 字體 */
@property (nonatomic, strong) UIFont         *titleFont;
/** 字體大小 */
@property (nonatomic, assign) CGFloat        titleFontSize;
/** 字體size */
@property (nonatomic, assign) CGSize         titleSize;
/** 圖標(biāo)字體 */
@property (nonatomic, strong) UIFont         *iconFont;
/** 圖標(biāo)字體大小 */
@property (nonatomic, assign) CGFloat        iconFontSize;
/** 圖標(biāo)size */
@property (nonatomic, assign) CGSize         iconSize;
/** 標(biāo)題顏色 */
@property (nonatomic, strong) UIColor        *titleColor;
/** 圖標(biāo)顏色 */
@property (nonatomic, strong) UIColor        *iconColor;

- (instancetype)initWithTitle:(NSString *)title  icon:(NSString *)icon iconType:(ButtonIconType)iconType;
- (void)setupUI;

@end

ZMButton.m

#import "ZMButton.h"

@implementation ZMButton

- (UILabel *)buttonTitleLabel{
    if (!_buttonTitleLabel) {
        _buttonTitleLabel = [[UILabel alloc] init];
        _buttonTitleLabel.font = _titleFont;
        _buttonTitleLabel.textColor = _titleColor;
        [self addSubview:_buttonTitleLabel];
        [_buttonTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            if (_iconType == ButtonIconTypeNormal) {
                make.left.mas_equalTo(self.marginLeft);
                make.centerY.mas_equalTo(self);
            }else if (_iconType == ButtonIconTypeLeft){
                make.centerY.mas_equalTo(self);
                make.left.mas_equalTo(self.buttonIconLabel.mas_right).with.offset(_margin);
                //make.right.mas_equalTo(-_margin);
            }else if (_iconType == ButtonIconTypeTop){
                make.top.mas_equalTo(self.buttonIconLabel.mas_bottom).with.offset(_margin);
                make.height.mas_equalTo(_titleSize.height);
                make.centerX.mas_equalTo(self);
            }else if (_iconType == ButtonIconTypeBottom){
                make.top.mas_equalTo((self.height - _titleSize.height - _iconSize.height - _margin)/2);
                make.height.mas_equalTo(_titleSize.height);
                make.centerX.mas_equalTo(self);
            }
        }];
    }
    return _buttonTitleLabel;
}

- (UILabel *)buttonIconLabel{
    if (!_buttonIconLabel) {
        _buttonIconLabel = [[UILabel alloc] init];
        _buttonIconLabel.font = [ZMFont iconOutlineFontWithSize:_iconFontSize];
        _buttonIconLabel.textColor = _iconColor;
        [self addSubview:_buttonIconLabel];
        [_buttonIconLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            if (_iconType == ButtonIconTypeNormal) {
                //make.right.mas_equalTo(-_margin);
                make.left.mas_equalTo(self.buttonTitleLabel.mas_right).with.offset(_margin);
                make.centerY.mas_equalTo(self);
            }else if (_iconType == ButtonIconTypeLeft){
                make.left.mas_equalTo(self.marginLeft);
                make.centerY.mas_equalTo(self);
            }else if (_iconType == ButtonIconTypeTop){
                make.top.mas_equalTo(self.marginTop);
                make.centerX.mas_equalTo(self);
                make.height.mas_equalTo(_iconSize.height);
            }else if (_iconType == ButtonIconTypeBottom){
                make.top.mas_equalTo(self.buttonTitleLabel.mas_bottom).with.offset(_margin);
                make.height.mas_equalTo(_iconSize.height);
                make.centerX.mas_equalTo(self);
            }
        }];
    }
    return _buttonIconLabel;
}

#pragma mark - 間距的get方法
- (CGFloat)marginLeft{
    return (self.width - _iconSize.width - _titleSize.width - _margin) /2;
}

- (CGFloat)marginTop{
    return (self.height - _titleSize.height - _iconSize.height - _margin)/2;
}

- (instancetype)initWithTitle:(NSString *)title  icon:(NSString *)icon iconType:(ButtonIconType)iconType{
    self = [super init];
    if (self) {
        self.layer.borderWidth = 1/YYScreenScale();
        self.layer.borderColor = [UIColor blackColor].CGColor;
        NSAssert(title.length, @"title is null");
        NSAssert(icon.length, @"icon is null");
        _buttonTitle = title;
        _buttonIcon  = icon;
        _iconType = iconType;
        _titleFontSize = 15;
        _iconFontSize = 15;
        _margin = 5;
        _titleFont = [UIFont systemFontOfSize:_titleFontSize];
        _iconFont = [ZMFont iconOutlineFontWithSize:_iconFontSize];
        _titleColor = [UIColor colorWithHexString:@"#757374"];
        _iconColor = [UIColor colorWithHexString:@"#757374"];
        
        _titleSize = [title sizeForFont:_titleFont size:CGSizeMake(kScreenWidth, _titleFontSize * 2) mode:NSLineBreakByWordWrapping];
        _iconSize = [icon sizeForFont:_iconFont size:CGSizeMake(kScreenWidth, _iconFontSize * 2) mode:0];
        
        [self getTotalWidth];
    }
    return self;
}

#pragma mark - 設(shè)置按鈕數(shù)據(jù)
- (void)setupUI{
    self.buttonTitleLabel.text = self.buttonTitle;
    self.buttonIconLabel.text = self.buttonIcon;
}

#pragma mark - 設(shè)置標(biāo)題數(shù)據(jù) (調(diào)用此方法可重寫設(shè)置按鈕標(biāo)題)
- (void)setButtonTitle:(NSString *)buttonTitle{
    if (buttonTitle.length) {
        _buttonTitle = buttonTitle;
        self.buttonTitleLabel.text = buttonTitle;
        _titleSize = [_buttonTitle sizeForFont:_titleFont size:CGSizeMake(kScreenWidth, _titleFontSize * 2) mode:NSLineBreakByWordWrapping];
        [self getTotalWidth];
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(self.totalWidth);
        }];
        [self.superview layoutIfNeeded];
        self.marginLeft = (self.width - _titleSize.width - _iconSize.width - _margin)/2;
        if (self.iconType == ButtonIconTypeNormal) {
            [self.buttonTitleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(self.marginLeft);
            }];
        }else if (self.iconType == ButtonIconTypeLeft) {
            [self.buttonIconLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(self.marginLeft);
            }];
        }
    }
}

#pragma mark - 計(jì)算總寬度
- (void)getTotalWidth{
    if (_iconType == ButtonIconTypeNormal || _iconType == ButtonIconTypeLeft) {
        self.totalWidth = _titleSize.width + _iconSize.width + _margin * 3;
    }else{
        self.totalWidth = _titleSize.width + _margin * 2;
    }
}

@end

4.如何使用:

//圖標(biāo)在左
    ZMButton *iconButtonLeft = [[ZMButton alloc] initWithTitle:@"文字在右" icon:@"\U0000e6df\U0000ea9b" iconType:ButtonIconTypeLeft];
    iconButtonLeft.margin = 10;
    iconButtonLeft.titleColor = [UIColor colorWithHexString:@"#DC143C"];
    iconButtonLeft.tag = 1;
    [self.view addSubview:iconButtonLeft];
    [iconButtonLeft mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.width.mas_equalTo(120);
        make.centerX.mas_equalTo(self.view);
    }];
    [iconButtonLeft.superview layoutIfNeeded];
    [iconButtonLeft setupUI];
    [iconButtonLeft addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    
    //圖標(biāo)在右
    ZMButton *iconButtonRight = [[ZMButton alloc] initWithTitle:@"文字在左" icon:@"\U0000e6df" iconType:ButtonIconTypeNormal];
    [self.view addSubview:iconButtonRight];
    [iconButtonRight mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(iconButtonLeft.mas_bottom).with.offset(20);
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(40);
    }];
    [iconButtonRight.superview layoutIfNeeded];
    [iconButtonRight setupUI];
    [iconButtonRight addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];

5.相關(guān)界面礁击,初始化按鈕之后盐杂,文本也可以隨時(shí)修改,支持多個(gè)圖標(biāo)哆窿,代碼中加入了FLEX,可以搖一搖試試~

image.png

6.代碼地址奉上:https://github.com/Brances/ZMProject,可能代碼寫的不是很好挚躯,還請(qǐng)各位大神多指教强衡。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市码荔,隨后出現(xiàn)的幾起案子漩勤,更是在濱河造成了極大的恐慌,老刑警劉巖缩搅,帶你破解...
    沈念sama閱讀 216,919評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件越败,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡硼瓣,警方通過查閱死者的電腦和手機(jī)究飞,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,567評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)巨双,“玉大人噪猾,你說我怎么就攤上這事≈郏” “怎么了?”我有些...
    開封第一講書人閱讀 163,316評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵丝蹭,是天一觀的道長(zhǎng)慢宗。 經(jīng)常有香客問我,道長(zhǎng),這世上最難降的妖魔是什么镜沽? 我笑而不...
    開封第一講書人閱讀 58,294評(píng)論 1 292
  • 正文 為了忘掉前任敏晤,我火速辦了婚禮,結(jié)果婚禮上缅茉,老公的妹妹穿的比我還像新娘嘴脾。我一直安慰自己,他們只是感情好蔬墩,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,318評(píng)論 6 390
  • 文/花漫 我一把揭開白布译打。 她就那樣靜靜地躺著,像睡著了一般拇颅。 火紅的嫁衣襯著肌膚如雪奏司。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,245評(píng)論 1 299
  • 那天樟插,我揣著相機(jī)與錄音韵洋,去河邊找鬼。 笑死黄锤,一個(gè)胖子當(dāng)著我的面吹牛搪缨,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播鸵熟,決...
    沈念sama閱讀 40,120評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼副编,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了旅赢?” 一聲冷哼從身側(cè)響起齿桃,我...
    開封第一講書人閱讀 38,964評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎煮盼,沒想到半個(gè)月后短纵,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,376評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡僵控,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,592評(píng)論 2 333
  • 正文 我和宋清朗相戀三年香到,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片报破。...
    茶點(diǎn)故事閱讀 39,764評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡悠就,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出充易,到底是詐尸還是另有隱情梗脾,我是刑警寧澤,帶...
    沈念sama閱讀 35,460評(píng)論 5 344
  • 正文 年R本政府宣布盹靴,位于F島的核電站炸茧,受9級(jí)特大地震影響瑞妇,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜梭冠,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,070評(píng)論 3 327
  • 文/蒙蒙 一辕狰、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧控漠,春花似錦蔓倍、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,697評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至毙驯,卻和暖如春倒堕,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背爆价。 一陣腳步聲響...
    開封第一講書人閱讀 32,846評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工垦巴, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人铭段。 一個(gè)月前我還...
    沈念sama閱讀 47,819評(píng)論 2 370
  • 正文 我出身青樓骤宣,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親序愚。 傳聞我的和親對(duì)象是個(gè)殘疾皇子憔披,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,665評(píng)論 2 354

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

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件爸吮、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,096評(píng)論 4 62
  • 在嘉興南湖的游船上 誕生至今已九十六載 歷史飽經(jīng)滄桑巨變 就在彈指一揮間 你邁著青春的腳步 從未蒼老的容顏 意氣風(fēng)...
    瀚正閱讀 304評(píng)論 0 2
  • 半川雪閱讀 75評(píng)論 0 0
  • 一芬膝、物理是一個(gè)整體浸锨,剛剛接觸管搪,可能對(duì)某些概念袍啡、規(guī)律掌握不好禀酱,對(duì)知識(shí)無(wú)整體感,這是正常的驻子。隨著學(xué)習(xí)后面相關(guān)的知識(shí)谭羔,對(duì)...
    hyhyhyaaaa閱讀 197評(píng)論 0 1
  • ps:寫于2014-6-27岂傲,我的百度空間哄酝。在美國(guó)時(shí)不知為何打不開百度空間友存,于是在簡(jiǎn)書安了家,希望將教學(xué)記錄整理在...
    素秋微菊閱讀 312評(píng)論 0 1