由于開發(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)一哈,