圖片加文字的居中顯示

由于開發(fā)中圖片文字居中顯示很多范咨,而且有時(shí)候還需要后臺(tái)返回?cái)?shù)據(jù)動(dòng)態(tài)改變氛谜,如果我們按照之前的imge+label模式寫君账,肯定會(huì)非常麻煩叔壤,而且不可控瞎饲。所以本人寫了一個(gè)簡單的button不論適配5還是最新的x,都非常好用炼绘,調(diào)用一句話就行嗅战。以下是代碼:
ZJButton.h

#import <UIKit/UIKit.h>
//typedef enum : NSInteger {
//    ZJ_BUTTONNORMAL,//正常
//    ZJ_BUTTONSPECIAL,//特殊
//    ZJ_BUTTONSTOREOPERATION,//門店管理
//    ZJ_BUTTONMYBUSINESS,//我的
//
//} BUTTONTYLE;

//#define WhiteSmoke [UIColor colorWithHex:@"#e7e7e7"]//DCDCDC
@interface ZJButton : UIButton
+(ZJButton *)shareBtn;


//@property(nonatomic,assign) BUTTONTYLE buttonStyle;



-(void)autoLayoutfNeedsWithBtn:(UIButton *)btn layerColor:(UIColor *)layerColor;
//view的layer設(shè)置
-(void)autoLayoutfNeedsWithView:(UIView *)view andRadius:(CGFloat)radius andBorderColor:(UIColor *)borderColor ;

//設(shè)置imageview在中心
-(void)setBtn:(UIButton *)btn andCenterImageName:(NSString *)imageName;


//設(shè)置方式為imageView(上)+label(下)
//設(shè)置方式為imageView(上)+label(下)
-(void)setBtn:(UIButton *)btn  andImageName:(NSString *)imageName andTitle:(NSString *)title;
//設(shè)置方式為label(上)+label(下)
-(void)setBtn:(UIButton *)btn WithCount:(NSString *)count andName:(NSString *)name nameColor:(UIColor *)nameColor andCountFont:(CGFloat)countfont andNameFont:(CGFloat)nameFont;
//添加btn超文本(文字右側(cè),圖片左側(cè))
//添加headbtn超文本
-(void)setAttributeBtn:(UIButton *)btn WithImagename:(NSString *)imagename andtitle:(NSString *)title  andTitleColor:(UIColor *)titlecolor andtitleFont:(CGFloat)fonts andimageBounds:(CGRect)bounds;
//添加btn超文本(圖片右側(cè)俺亮,文字左側(cè))
-(void)setAttrBtn:(UIButton *)headBtn andTitleColor:(UIColor *)titleColor withRightImagename:(NSString *)imagename andLefttitle:(NSString *)title;
//添加btn超文本(title默認(rèn)顏色,colorTitle自定義顏色)
-(void)setAttributeBtn:(UIButton *)headBtn WithTitle:(NSString *)title andClolrTitle:(NSString *)colorTitle andColor:(UIColor *)color;
-(instancetype)initWithLefTitle:(NSString *)title andTitleColor:(UIColor *)titlecolor andtitleFont:(CGFloat)fonts andRightImageName:(NSString *)imageName;

//設(shè)置圖片(上)+文字(下)
-(void)setbtn:(UIButton *)btn andImageName:(NSString *)imagename andTitle:(NSString *)title andColor:(UIColor *)titleColor andFont:(CGFloat)font;


//設(shè)置方式為imageView(上)+label(下)
-(void)setbtn:(UIButton *)btn andImageName:(NSString *)imagename andTitle:(NSString *)title andColor:(UIColor *)titleColor andFont:(CGFloat)font andScale:(CGFloat)scale;
@end

ZJButton.m

//
//  ZJButton.m
//  IM
//
//  Created by xx on 2017/5/18.
//  Copyright ? 2017年 Michael Hu. All rights reserved.
//

#import "ZJButton.h"
#import "NSAttributedString+YYText.h"


@implementation ZJButton
//創(chuàng)建單例
+(ZJButton *)shareBtn{
    static ZJButton *btn = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        btn = [[self alloc] init];
    });
    return btn;
}


-(instancetype)init{
    self = [super init];
    return self;
}

//設(shè)置方式為imageView(上)+label(下)
-(void)setBtn:(UIButton *)btn  andImageName:(NSString *)imageName andTitle:(NSString *)title{
    UIImageView *iconView = [[UIImageView alloc] init];
    iconView.image = [UIImage imageNamed:imageName];
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.text = title;
    nameLabel.font = [UIFont systemFontOfSize:15];
    [nameLabel sizeToFit];

    [btn addSubview:nameLabel];
    [btn addSubview:iconView];
    
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(btn);
        make.bottom.equalTo(btn).offset(-10);
        make.height.equalTo(@20);
    }];
    
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(btn);
        make.top.equalTo(btn).offset(15);
        make.bottom.equalTo(nameLabel.mas_top).offset(-10);
        make.width.equalTo(iconView.mas_height);
    }];
    
}


//設(shè)置方式為label(上)+label(下)
-(void)setBtn:(UIButton *)btn WithCount:(NSString *)count andName:(NSString *)name nameColor:(UIColor *)nameColor andCountFont:(CGFloat)countfont andNameFont:(CGFloat)nameFont{
    [btn layoutIfNeeded];
    //創(chuàng)建兩個(gè)label
    UILabel *moneyLabel = [[UILabel alloc] init];
    UILabel *nameLabel = [[UILabel alloc] init];
    //設(shè)置label的textColor
    moneyLabel.textColor = [UIColor whiteColor];
    nameLabel.textColor = nameColor;
    //設(shè)置label的font
    moneyLabel.font = [UIFont fontWithName:@"AmericanTypewriter-Bold"size:countfont];
    nameLabel.font = [UIFont systemFontOfSize:nameFont];
    //設(shè)置label的text
    moneyLabel.text = count;
    nameLabel.text = name;
    //自動(dòng)填充
    [moneyLabel sizeToFit];
    [nameLabel sizeToFit];
    //添加
    [btn addSubview:moneyLabel];
    [btn addSubview:nameLabel];
    
    //自動(dòng)布局
    [moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(btn);
        make.bottom.equalTo(btn.mas_centerY);
        make.centerX.equalTo(btn);
    }];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(btn.mas_centerY);
        make.centerX.equalTo(btn);
    }];
    
}
#pragma mark 添加headbtn超文本
-(instancetype)initWithLefTitle:(NSString *)title andTitleColor:(UIColor *)titlecolor andtitleFont:(CGFloat)fonts andRightImageName:(NSString *)imageName{
    if (self = [super init]) {
        // 添加表情
        NSTextAttachment *attch = [[NSTextAttachment alloc] init];
        // 表情圖片
        attch.image = [UIImage imageNamed:imageName];
        // 設(shè)置圖片大小
        attch.bounds = CGRectMake(0, -5, 20, 20);
        // 創(chuàng)建帶有圖片的富文本
        NSMutableAttributedString *string = (NSMutableAttributedString *)[NSAttributedString attributedStringWithAttachment:attch];
        
        NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"  %@",title]];
        
        //yytext方法
        //設(shè)置超文本的字體顏色
        attri.color = titlecolor;
        //設(shè)置超文本的字體大小
        attri.font = [UIFont systemFontOfSize:fonts];
        [string appendAttributedString:attri];
        
        [self setAttributedTitle:string forState:UIControlStateNormal];
        [self sizeToFit];
    }
    return self;
}
//添加headbtn超文本
-(void)setAttributeBtn:(UIButton *)btn WithImagename:(NSString *)imagename andtitle:(NSString *)title  andTitleColor:(UIColor *)titlecolor andtitleFont:(CGFloat)fonts andimageBounds:(CGRect)bounds{
    
    // 添加表情
    NSTextAttachment *attch = [[NSTextAttachment alloc] init];
    // 表情圖片
    attch.image = [UIImage imageNamed:imagename];
    // 設(shè)置圖片大小
    attch.bounds = bounds;
    // 創(chuàng)建帶有圖片的富文本
    NSMutableAttributedString *string = (NSMutableAttributedString *)[NSAttributedString attributedStringWithAttachment:attch];
    
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"  %@",title]];
    
    //yytext方法
    //設(shè)置超文本的字體顏色
    attri.color = titlecolor;
    //設(shè)置超文本的字體大小
    attri.font = [UIFont systemFontOfSize:fonts];
    [string appendAttributedString:attri];
    [btn setAttributedTitle:string forState:UIControlStateNormal];
}

-(void)setAttributeBtn:(UIButton *)headBtn WithTitle:(NSString *)title andClolrTitle:(NSString *)colorTitle andColor:(UIColor *)color{
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",title]];
    // 創(chuàng)建帶有圖片的富文本
    NSMutableAttributedString *attri1 = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",colorTitle]];
    attri1.color = color;
    
    [attri appendAttributedString:attri1];
    
    [headBtn setAttributedTitle:attri forState:UIControlStateNormal];
}

//添加headbtn超文本
-(void)setAttrBtn:(UIButton *)headBtn andTitleColor:(UIColor *)titleColor withRightImagename:(NSString *)imagename andLefttitle:(NSString *)title {
    
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ ",title]];
    attri.color = titleColor;
    attri.font = [UIFont systemFontOfSize:13];
    // 添加表情
    NSTextAttachment *attch = [[NSTextAttachment alloc] init];
    // 表情圖片
    attch.image = [UIImage imageNamed:imagename];
    // 設(shè)置圖片大小
    // attch.bounds = CGRectMake(0, 0, 20, 20);
    // 創(chuàng)建帶有圖片的富文本
    NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];
    
    [attri appendAttributedString:string];
    
    [headBtn setAttributedTitle:attri forState:UIControlStateNormal];
}


-(void)setbtn:(UIButton *)btn andImageName:(NSString *)imagename andTitle:(NSString *)title andColor:(UIColor *)titleColor andFont:(CGFloat)font{
    [self autoLayoutfNeedsWithBtn:btn layerColor:[UIColor clearColor]];
    CGFloat width = btn.frame.size.width;
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:imagename];
    
    UILabel *label = [[UILabel alloc] init];
    label.text = title;
    label.textColor = titleColor;
    label.font = [UIFont systemFontOfSize:font];
    [label sizeToFit];
    
    [btn addSubview:imageView];
    [btn addSubview:label];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(btn).mas_offset(width*0.15);
        make.centerX.equalTo(btn);
        make.width.height.mas_equalTo(width*0.4);
    }];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageView.mas_bottom).mas_offset(width*0.1);
        make.centerX.equalTo(imageView);
        make.bottom.equalTo(btn).mas_offset(-(width*0.15));
    }];
    
}



#pragma mark btnlayout
-(void)autoLayoutfNeedsWithBtn:(UIButton *)btn layerColor:(UIColor *)layerColor{
    [btn layoutIfNeeded];
    btn.layer.borderColor = layerColor.CGColor;
    btn.layer.borderWidth = 0.4;
    btn.layer.cornerRadius = 2;
    btn.layer.masksToBounds = YES;
}
-(void)autoLayoutfNeedsWithView:(UIView *)view andRadius:(CGFloat)radius andBorderColor:(UIColor *)borderColor {
    [view layoutIfNeeded];
    view.layer.borderColor = borderColor.CGColor;
    view.layer.borderWidth = 0.4;
    view.layer.cornerRadius = radius;
    view.layer.masksToBounds = YES;
}


-(void)setBtn:(UIButton *)btn andCenterImageName:(NSString *)imageName{
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.image = [UIImage imageNamed:imageName];
    [btn addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(btn).offset(5);
        make.right.bottom.equalTo(btn).offset(-5);
    }];
}


//設(shè)置方式為imageView(上)+label(下)
/*
 *
 * @param btn 添加的btn
 *
 * @param imagename 圖片名稱
 *
 * @param title title名稱
 *
 * @param font 字體
 *
 * @param scale 寬高比
 */
-(void)setbtn:(UIButton *)btn andImageName:(NSString *)imagename andTitle:(NSString *)title andColor:(UIColor *)titleColor andFont:(CGFloat)font andScale:(CGFloat)scale{
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    CGFloat height = btn.frame.size.height;
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:imagename];
    
    UILabel *label = [[UILabel alloc] init];
    label.text = title;
    label.textColor = titleColor;
    label.font = [UIFont systemFontOfSize:font];
    [label sizeToFit];
    
    [btn addSubview:imageView];
    [btn addSubview:label];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(btn).mas_offset(height*0.15);
        make.centerX.equalTo(btn);
        make.height.mas_equalTo(height*0.4);
        make.width.mas_equalTo(height*0.4*scale);
    }];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageView.mas_bottom).mas_offset(height*0.1);
        make.centerX.equalTo(imageView);
        make.bottom.equalTo(btn).mas_offset(-(height*0.15));
    }];
    
}
@end

你可以創(chuàng)建這兩個(gè).h和.m文件驮捍,進(jìn)去體驗(yàn)一哈,

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末脚曾,一起剝皮案震驚了整個(gè)濱河市东且,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌本讥,老刑警劉巖珊泳,帶你破解...
    沈念sama閱讀 212,029評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異拷沸,居然都是意外死亡色查,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,395評論 3 385
  • 文/潘曉璐 我一進(jìn)店門撞芍,熙熙樓的掌柜王于貴愁眉苦臉地迎上來秧了,“玉大人,你說我怎么就攤上這事序无⊙檎保” “怎么了?”我有些...
    開封第一講書人閱讀 157,570評論 0 348
  • 文/不壞的土叔 我叫張陵愉镰,是天一觀的道長米罚。 經(jīng)常有香客問我,道長丈探,這世上最難降的妖魔是什么录择? 我笑而不...
    開封第一講書人閱讀 56,535評論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮碗降,結(jié)果婚禮上隘竭,老公的妹妹穿的比我還像新娘。我一直安慰自己讼渊,他們只是感情好动看,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,650評論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著爪幻,像睡著了一般菱皆。 火紅的嫁衣襯著肌膚如雪须误。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,850評論 1 290
  • 那天仇轻,我揣著相機(jī)與錄音京痢,去河邊找鬼。 笑死篷店,一個(gè)胖子當(dāng)著我的面吹牛祭椰,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播疲陕,決...
    沈念sama閱讀 39,006評論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼方淤,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了蹄殃?” 一聲冷哼從身側(cè)響起携茂,我...
    開封第一講書人閱讀 37,747評論 0 268
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎窃爷,沒想到半個(gè)月后邑蒋,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,207評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡按厘,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,536評論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了钱慢。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片逮京。...
    茶點(diǎn)故事閱讀 38,683評論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖束莫,靈堂內(nèi)的尸體忽然破棺而出懒棉,到底是詐尸還是另有隱情,我是刑警寧澤览绿,帶...
    沈念sama閱讀 34,342評論 4 330
  • 正文 年R本政府宣布策严,位于F島的核電站,受9級特大地震影響饿敲,放射性物質(zhì)發(fā)生泄漏妻导。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,964評論 3 315
  • 文/蒙蒙 一怀各、第九天 我趴在偏房一處隱蔽的房頂上張望倔韭。 院中可真熱鬧,春花似錦瓢对、人聲如沸寿酌。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,772評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽醇疼。三九已至硕并,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間秧荆,已是汗流浹背鲤孵。 一陣腳步聲響...
    開封第一講書人閱讀 32,004評論 1 266
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留辰如,地道東北人普监。 一個(gè)月前我還...
    沈念sama閱讀 46,401評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像琉兜,于是被迫代替她去往敵國和親凯正。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,566評論 2 349

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

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫豌蟋、插件廊散、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,066評論 4 62
  • 2018-09-17 星期一,雨 在同學(xué)群里梧疲,我默默的關(guān)注著大家的回憶嬉笑允睹,都這么大歲數(shù)了,大家都對當(dāng)年的青澀朦...
    梅月影閱讀 985評論 9 38
  • 有時(shí)候你把全部交給他人,得到了信任该互,卻也失去了自保的能力米者。很多投資者還是比較相信的自己的老師的,也跟老師有...
    戴強(qiáng)策金閱讀 77評論 0 0