图片加文字的居中显示

由于开发中图片文字居中显示很多,而且有时候还需要后台返回数据动态改变,如果我们按照之前的imge+label模式写,肯定会非常麻烦,而且不可控。所以本人写了一个简单的button不论适配5还是最新的x,都非常好用,调用一句话就行。以下是代码:
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设置
-(void)autoLayoutfNeedsWithView:(UIView *)view andRadius:(CGFloat)radius andBorderColor:(UIColor *)borderColor ;

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


//设置方式为imageView(上)+label(下)
//设置方式为imageView(上)+label(下)
-(void)setBtn:(UIButton *)btn  andImageName:(NSString *)imageName andTitle:(NSString *)title;
//设置方式为label(上)+label(下)
-(void)setBtn:(UIButton *)btn WithCount:(NSString *)count andName:(NSString *)name nameColor:(UIColor *)nameColor andCountFont:(CGFloat)countfont andNameFont:(CGFloat)nameFont;
//添加btn超文本(文字右侧,图片左侧)
//添加headbtn超文本
-(void)setAttributeBtn:(UIButton *)btn WithImagename:(NSString *)imagename andtitle:(NSString *)title  andTitleColor:(UIColor *)titlecolor andtitleFont:(CGFloat)fonts andimageBounds:(CGRect)bounds;
//添加btn超文本(图片右侧,文字左侧)
-(void)setAttrBtn:(UIButton *)headBtn andTitleColor:(UIColor *)titleColor withRightImagename:(NSString *)imagename andLefttitle:(NSString *)title;
//添加btn超文本(title默认颜色,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;

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


//设置方式为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
//创建单例
+(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;
}

//设置方式为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);
    }];
    
}


//设置方式为label(上)+label(下)
-(void)setBtn:(UIButton *)btn WithCount:(NSString *)count andName:(NSString *)name nameColor:(UIColor *)nameColor andCountFont:(CGFloat)countfont andNameFont:(CGFloat)nameFont{
    [btn layoutIfNeeded];
    //创建两个label
    UILabel *moneyLabel = [[UILabel alloc] init];
    UILabel *nameLabel = [[UILabel alloc] init];
    //设置label的textColor
    moneyLabel.textColor = [UIColor whiteColor];
    nameLabel.textColor = nameColor;
    //设置label的font
    moneyLabel.font = [UIFont fontWithName:@"AmericanTypewriter-Bold"size:countfont];
    nameLabel.font = [UIFont systemFontOfSize:nameFont];
    //设置label的text
    moneyLabel.text = count;
    nameLabel.text = name;
    //自动填充
    [moneyLabel sizeToFit];
    [nameLabel sizeToFit];
    //添加
    [btn addSubview:moneyLabel];
    [btn addSubview:nameLabel];
    
    //自动布局
    [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];
        // 设置图片大小
        attch.bounds = CGRectMake(0, -5, 20, 20);
        // 创建带有图片的富文本
        NSMutableAttributedString *string = (NSMutableAttributedString *)[NSAttributedString attributedStringWithAttachment:attch];
        
        NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"  %@",title]];
        
        //yytext方法
        //设置超文本的字体颜色
        attri.color = titlecolor;
        //设置超文本的字体大小
        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];
    // 设置图片大小
    attch.bounds = bounds;
    // 创建带有图片的富文本
    NSMutableAttributedString *string = (NSMutableAttributedString *)[NSAttributedString attributedStringWithAttachment:attch];
    
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"  %@",title]];
    
    //yytext方法
    //设置超文本的字体颜色
    attri.color = titlecolor;
    //设置超文本的字体大小
    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]];
    // 创建带有图片的富文本
    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];
    // 设置图片大小
    // attch.bounds = CGRectMake(0, 0, 20, 20);
    // 创建带有图片的富文本
    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);
    }];
}


//设置方式为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

你可以创建这两个.h和.m文件,进去体验一哈,

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 205,033评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,725评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,473评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,846评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,848评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,691评论 1 282
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,053评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,700评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,856评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,676评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,787评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,430评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,034评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,990评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,218评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,174评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,526评论 2 343

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,019评论 4 62
  • 2018-09-17 星期一,雨 在同学群里,我默默的关注着大家的回忆嬉笑,都这么大岁数了,大家都对当年的青涩朦...
    梅月影阅读 981评论 9 38
  • 有时候你把全部交给他人,得到了信任,却也失去了自保的能力。很多投资者还是比较相信的自己的老师的,也跟老师有...
    戴强策金阅读 75评论 0 0