CustomBtn.h
typedef NS_ENUM(NSInteger,BtnType)
{
BT_LeftImg =1,// 左图右字
BT_TopImg,// 上图下字
BT_RightImg,// 右图左字
BT_BottomImg,// 下图上字
BT_OnlyImg,// 单图
BT_OnlyText// 单字
};
#import <UIKit/UIKit.h>
@interface CustomBtn : UIButton
@property (nonatomic,assign) BtnType type;
@property (nonatomic,copy) NSString * selectImg; //选中的背景图
@property (nonatomic,copy) NSString * normalImg; //正常情况下的背景图
@property (nonatomic,assign) CGSize imgSize;
@property (nonatomic,copy) NSString * text;//文字
@property (nonatomic,strong) UIFont * textFont;//字号
@property (nonatomic,strong) UIColor * selectTextColor;//选中时的字体颜色
@property (nonatomic,strong) UIColor * normalTextColor;//正常时字体颜色
@property (nonatomic,assign) NSTextAlignment textAlign; //默认居中
@property (nonatomic,assign) CGFloat textImgGap;//字图间隔
@property (nonatomic,assign) BOOL isSelected;
@property (nonatomic,assign) UIEdgeInsets contentEdge;
@property (nonatomic,strong) UIColor * normalBgColor;//非选中时的背景色
@property (nonatomic,strong) UIColor * selectBgColor;//选中时的背景色
@end
CustomBtn.m
#import "CustomBtn.h"
@interface CustomBtn ()
@property (nonatomic,strong) UIImageView * imgView;
@property (nonatomic,strong) UILabel * textLBL;
@end
@implementation CustomBtn
-(instancetype)init
{
if(self=[superinit])
{
//默认值
_imgSize=CGSizeMake(20,20);
_textImgGap=4;
_contentEdge=UIEdgeInsetsMake(0,0,0,0);
_selectBgColor = _normalBgColor = [UIColor whiteColor];
_imgView= [UIImageViewnew];
[selfaddSubview:_imgView];
_textLBL= [UILabelnew];
_textLBL.font=kFont_12;
_textLBL.textColor= [UIColorblackColor];
_textLBL.textAlignment = NSTextAlignmentCenter;
[selfaddSubview:_textLBL];
}
return self;
}
-(void)setType:(BtnType)type
{
_type= type;
[self upDateContentLayout];
}
-(void)setSelectImg:(NSString*)selectImg
{
_selectImg= selectImg;
}
-(void)setNormalImg:(NSString*)normalImg
{
_normalImg= normalImg;
_imgView.image= [UIImageimageNamed:normalImg];
}
-(void)setImgSize:(CGSize)imgSize
{
_imgSize= imgSize;
}
-(void)setText:(NSString*)text
{
_textLBL.text= text;
}
-(void)setTextFont:(UIFont*)textFont
{
_textLBL.font= textFont;
}
-(void)setNormalTextColor:(UIColor*)normalTextColor
{
_normalTextColor= normalTextColor;
}
-(void)setSelectTextColor:(UIColor*)selectTextColor
{
_selectTextColor= selectTextColor;
}
-(void)setTextAlign:(NSTextAlignment)textAlign
{
_textLBL.textAlignment= textAlign;
}
-(void)setTextImgGap:(CGFloat)textImgGap
{
_textImgGap= textImgGap;
}
-(void)setContentEdge:(UIEdgeInsets)contentEdge
{
_contentEdge= contentEdge;
}
-(void)setNormalBgColor:(UIColor*)normalBgColor
{
_normalBgColor= normalBgColor;
}
-(void)setSelectBgColor:(UIColor*)selectBgColor
{
_selectBgColor= selectBgColor;
}
-(void)setIsSelected:(BOOL)isSelected
{
_isSelected= isSelected;
//图片
NSString* imgStr = isSelected ?_selectImg:_normalImg;
if(imgStr.length!=0)
{
self.imgView.image= [UIImageimageNamed:imgStr];
}
//字体颜色
self.textLBL.textColor = isSelected ? _selectTextColor:_normalTextColor;
//背景色
self.backgroundColor = isSelected ? _selectBgColor:_normalBgColor;
}
//更新布局
-(void)upDateContentLayout
{
switch(_type)
{
caseBT_LeftImg://左图右字
{
[_imgView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self.mas_centerY);
make.left.mas_equalTo(self.mas_left).offset(_contentEdge.left);
make.size.mas_equalTo(_imgSize);
}];
[_textLBL mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(_imgView.mas_right).offset(_textImgGap);
make.centerY.mas_equalTo(self.mas_centerY);
make.right.mas_equalTo(self.mas_right);
make.height.mas_greaterThanOrEqualTo(0);
}];
}
break;
caseBT_TopImg://上图下字
{
[_imgView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.mas_top).offset(_contentEdge.top);
make.centerX.mas_equalTo(self.mas_centerX);
make.size.mas_equalTo(_imgSize);
}];
[_textLBL mas_remakeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.mas_centerX);
make.top.mas_equalTo(_imgView.mas_bottom).offset(_textImgGap);
make.width.height.mas_greaterThanOrEqualTo(0);
}];
}
break;
caseBT_RightImg://右图左字
{
[_imgView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self.mas_centerY);
make.right.mas_equalTo(self.mas_right).offset(_contentEdge.right);
make.size.mas_equalTo(_imgSize);
}];
[_textLBL mas_remakeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(_imgView.mas_left).offset(-_textImgGap);
make.centerY.mas_equalTo(self.mas_centerY);
make.left.mas_equalTo(self.mas_left);
make.height.mas_greaterThanOrEqualTo(0);
}];
}
break;
caseBT_BottomImg://上字下图
{
[_imgView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.mas_centerX);
make.bottom.mas_equalTo(self.mas_bottom).offset(_contentEdge.bottom);
make.size.mas_equalTo(_imgSize);
}];
[_textLBL mas_remakeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.mas_centerX);
make.bottom.mas_equalTo(_imgView.mas_top).offset(-_textImgGap);
make.width.height.mas_greaterThanOrEqualTo(0);
}];
}
break;
caseBT_OnlyImg://单图
{
[_imgView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.mas_centerX);
make.centerY.mas_equalTo(self.mas_centerY);
make.size.mas_equalTo(_imgSize);
}];
}
break;
caseBT_OnlyText://单字
{
[_textLBL mas_remakeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.mas_centerX);
make.centerY.mas_equalTo(self.mas_centerY);
make.width.height.mas_greaterThanOrEqualTo(0);
}];
}
break;
default:
break;
}
}
使用详例:
- (void)viewDidLoad
{
[super viewDidLoad];
[self setNavBackBtnImg:@"" Title:@"资讯"];
//上图下字
CustomBtn* topImgBtn = [[CustomBtnalloc]init];
topImgBtn.selectImg= topImgBtn.normalImg=@"TestBtnImg";
topImgBtn.imgSize=CGSizeMake(26,26);
topImgBtn.contentEdge=UIEdgeInsetsMake(3,0,0,0);
topImgBtn.textImgGap=6;
topImgBtn.text=@"这是文字内容";
topImgBtn.selectTextColor= topImgBtn.normalTextColor=kColor_Black;
topImgBtn.textFont=kFont_10;
topImgBtn.type=BT_TopImg;
//topImgBtn.backgroundColor = [UIColor orangeColor];
topImgBtn.tag=1;
[topImgBtnaddTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];
[self.containerViewaddSubview:topImgBtn];
[topImgBtnmas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.containerView.mas_centerX);
make.top.mas_equalTo(self.containerView.mas_top).offset(40);
make.size.mas_equalTo(CGSizeMake(70,50));
}];
//左图右字
CustomBtn* leftImgBtn = [[CustomBtnalloc]init];
leftImgBtn.selectImg = @"TestBtn_RememberPwd_Select";
leftImgBtn.normalImg = @"TestBtn_RememberPwd_Non";
leftImgBtn.imgSize=CGSizeMake(12,12);
leftImgBtn.contentEdge=UIEdgeInsetsMake(0,2,0,0);
leftImgBtn.textImgGap=6;
leftImgBtn.text=@"记住密码";
leftImgBtn.textAlign=NSTextAlignmentLeft;
leftImgBtn.selectTextColor= leftImgBtn.normalTextColor=kColor_Black;
leftImgBtn.textFont=kFont_12;
leftImgBtn.isSelected=NO;
leftImgBtn.type=BT_LeftImg;
//leftImgBtn.backgroundColor = [UIColor orangeColor];
leftImgBtn.tag=2;
[leftImgBtnaddTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];
[self.containerViewaddSubview:leftImgBtn];
[leftImgBtnmas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.containerView.mas_centerX);
make.top.mas_equalTo(topImgBtn.mas_bottom).offset(30);
make.size.mas_equalTo(CGSizeMake(75,20));
}];
//下图上字
CustomBtn* bottomImgBtn = [[CustomBtnalloc]init];
bottomImgBtn.selectImg= bottomImgBtn.normalImg=@"TestBtnImg";
bottomImgBtn.imgSize=CGSizeMake(26,26);
bottomImgBtn.contentEdge=UIEdgeInsetsMake(0,0, -3,0);
bottomImgBtn.textImgGap=6;
bottomImgBtn.text=@"这是文字内容";
bottomImgBtn.selectTextColor= topImgBtn.normalTextColor=kColor_Black;
bottomImgBtn.textFont=kFont_10;
bottomImgBtn.type=BT_BottomImg;
// bottomImgBtn.backgroundColor = [UIColor orangeColor];
bottomImgBtn.tag=3;
[bottomImgBtnaddTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];
[self.containerViewaddSubview:bottomImgBtn];
[bottomImgBtnmas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.containerView.mas_centerX);
make.top.mas_equalTo(leftImgBtn.mas_top).offset(30);
make.size.mas_equalTo(CGSizeMake(70,50));
}];
//右图左字
CustomBtn* rightImgBtn = [[CustomBtnalloc]init];
rightImgBtn.selectImg= rightImgBtn.normalImg=@"TestBtn_Arrow";
rightImgBtn.imgSize=CGSizeMake(10,15);
rightImgBtn.textImgGap=6;
rightImgBtn.text=@"全部订单";
rightImgBtn.selectTextColor= rightImgBtn.normalTextColor=kColor_Black;
rightImgBtn.textFont=kFont_10;
rightImgBtn.type=BT_RightImg;
//rightImgBtn.backgroundColor = [UIColor orangeColor];
rightImgBtn.tag=4;
[rightImgBtnaddTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];
[self.containerViewaddSubview:rightImgBtn];
[rightImgBtnmas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.containerView.mas_centerX);
make.top.mas_equalTo(bottomImgBtn.mas_bottom).offset(30);
make.size.mas_equalTo(CGSizeMake(60,20));
}];
//单图
CustomBtn* onlyImgBtn = [[CustomBtnalloc]init];
onlyImgBtn.selectImg = @"TestBtn_Add_Select";
onlyImgBtn.normalImg=@"TestBtn_Add_Non";
onlyImgBtn.imgSize=CGSizeMake(14,14);
onlyImgBtn.isSelected=YES;
onlyImgBtn.type=BT_OnlyImg;
onlyImgBtn.tag=4;
[onlyImgBtnaddTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];
onlyImgBtn.backgroundColor= [UIColorblackColor];
[self.containerViewaddSubview:onlyImgBtn];
[onlyImgBtnmas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.containerView.mas_centerX);
make.top.mas_equalTo(rightImgBtn.mas_bottom).offset(30);
make.size.mas_equalTo(CGSizeMake(40,40));
}];
//单字
CustomBtn* onlyTextBtn = [[CustomBtnalloc]init];
onlyTextBtn.text=@"18点前";
onlyTextBtn.textFont=kFont_12;
onlyTextBtn.textAlign = NSTextAlignmentCenter;
onlyTextBtn.normalTextColor= onlyTextBtn.selectTextColor= [UIColorwhiteColor];
onlyTextBtn.normalBgColor=rgba(153,153,153,1);
onlyTextBtn.selectBgColor=kColor_Red;
onlyTextBtn.isSelected=NO;
onlyTextBtn.type=BT_OnlyText;
onlyTextBtn.tag=5;
onlyTextBtn.layer.cornerRadius=9.0;
onlyTextBtn.layer.masksToBounds=YES;
[onlyTextBtnaddTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];
[self.containerViewaddSubview:onlyTextBtn];
[onlyTextBtnmas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.containerView.mas_centerX);
make.top.mas_equalTo(onlyImgBtn.mas_bottom).offset(30);
make.size.mas_equalTo(CGSizeMake(60,18));
}];
}
-(void)btnClick:(CustomBtn*)sender
{
NSSLog(@"点击的sender.tag=%ld",(long)sender.tag);
if(sender.tag==2|| sender.tag==5)
{
sender.isSelected= !sender.isSelected;
}
}
网友评论