(按钮)上面为图片下部为文字的 按钮 控件
2016-05-25 18:48
405 查看
有的时候在做一些基础控件的时候系统的不能满足需要,就需要自己自定义一些控件了,这个按钮是上面显示图片,下半部分显示文字.
#import
@interface CollectView : UIView
@property(nonatomic,copy) NSString*
title;
@property(nonatomic,strong) UIColor*
titleColor;
+(instancetype) collectView;
-(void) addTarget:(id) target action:(SEL)action;
@end
#import "CollectView.h"
@interface CollectView()
@property(nonatomic,strong) UIButton*
topBtn;
@property(nonatomic,strong) UILabel*
titleLabel;
@end
@implementation CollectView
-(instancetype)initWithFrame:(CGRect)frame
{
self=[super initWithFrame:frame];
if (self) {
// 添加按钮
self.topBtn=[[UIButton alloc] init];
[self.topBtn setImage:[UIImage imageNamed:@"collect"]forState:UIControlStateNormal];
[self.topBtn setAdjustsImageWhenHighlighted:NO];
[self addSubview:self.topBtn];
// 添加Label
self.titleLabel=[[UILabel alloc] init];
self.titleLabel.textAlignment=NSTextAlignmentCenter;
self.titleLabel.font=[UIFont systemFontOfSize:12];
[self addSubview:self.titleLabel];
}
return self;
}
-(void)layoutSubviews
{
CGFloat W=self.bounds.size.height;
CGFloat btnX=0;
CGFloat btnY=0;
CGFloat btnW=self.bounds.size.width;
CGFloat btnH=W;
self.topBtn.frame=CGRectMake(btnX,
btnY, btnW, btnH);
UIEdgeInsets imageInsert=UIEdgeInsetsMake(0, 0,
W * 0.33, 0);
self.topBtn.imageEdgeInsets=imageInsert;
CGFloat labelX=0;
CGFloat labelY=W*0.66;//CGRectGetMaxY(self.topBtn.frame);
CGFloat labelW=self.bounds.size.width;
CGFloat labelH=W * 0.33;
self.titleLabel.frame=CGRectMake(labelX,
labelY, labelW, labelH);
}
-(void)setTitle:(NSString *)title
{
_title=title;
self.titleLabel.text=title;
}
-(void)setTitleColor:(UIColor *)titleColor
{
_titleColor=titleColor;
self.titleLabel.textColor=titleColor;
}
+(instancetype) collectView
{
return [[self alloc] init];
}
-(void) addTarget:(id)target action:(SEL)action
{
[self.topBtn addTarget:target action:actionforControlEvents:UIControlEventTouchUpInside];
}
//在控制器中使用自定义按钮
CGFloat collectW=50;
CGFloat collectX=kScreenWidth-kStationDetailBorder-collectW;
CGFloat collectY=infoLabelY;
CGFloat collectH=50;
_collectView=[CollectView collectView];
_collectView.frame=CGRectMake(collectX,
collectY, collectW, collectH);
_collectView.title=@"收藏";
[_stationInfoView addSubview:_collectView];
[_collectView addTarget:self action:@selector(collect)];
#import
@interface CollectView : UIView
@property(nonatomic,copy) NSString*
title;
@property(nonatomic,strong) UIColor*
titleColor;
+(instancetype) collectView;
-(void) addTarget:(id) target action:(SEL)action;
@end
#import "CollectView.h"
@interface CollectView()
@property(nonatomic,strong) UIButton*
topBtn;
@property(nonatomic,strong) UILabel*
titleLabel;
@end
@implementation CollectView
-(instancetype)initWithFrame:(CGRect)frame
{
self=[super initWithFrame:frame];
if (self) {
// 添加按钮
self.topBtn=[[UIButton alloc] init];
[self.topBtn setImage:[UIImage imageNamed:@"collect"]forState:UIControlStateNormal];
[self.topBtn setAdjustsImageWhenHighlighted:NO];
[self addSubview:self.topBtn];
// 添加Label
self.titleLabel=[[UILabel alloc] init];
self.titleLabel.textAlignment=NSTextAlignmentCenter;
self.titleLabel.font=[UIFont systemFontOfSize:12];
[self addSubview:self.titleLabel];
}
return self;
}
-(void)layoutSubviews
{
CGFloat W=self.bounds.size.height;
CGFloat btnX=0;
CGFloat btnY=0;
CGFloat btnW=self.bounds.size.width;
CGFloat btnH=W;
self.topBtn.frame=CGRectMake(btnX,
btnY, btnW, btnH);
UIEdgeInsets imageInsert=UIEdgeInsetsMake(0, 0,
W * 0.33, 0);
self.topBtn.imageEdgeInsets=imageInsert;
CGFloat labelX=0;
CGFloat labelY=W*0.66;//CGRectGetMaxY(self.topBtn.frame);
CGFloat labelW=self.bounds.size.width;
CGFloat labelH=W * 0.33;
self.titleLabel.frame=CGRectMake(labelX,
labelY, labelW, labelH);
}
-(void)setTitle:(NSString *)title
{
_title=title;
self.titleLabel.text=title;
}
-(void)setTitleColor:(UIColor *)titleColor
{
_titleColor=titleColor;
self.titleLabel.textColor=titleColor;
}
+(instancetype) collectView
{
return [[self alloc] init];
}
-(void) addTarget:(id)target action:(SEL)action
{
[self.topBtn addTarget:target action:actionforControlEvents:UIControlEventTouchUpInside];
}
//在控制器中使用自定义按钮
CGFloat collectW=50;
CGFloat collectX=kScreenWidth-kStationDetailBorder-collectW;
CGFloat collectY=infoLabelY;
CGFloat collectH=50;
_collectView=[CollectView collectView];
_collectView.frame=CGRectMake(collectX,
collectY, collectW, collectH);
_collectView.title=@"收藏";
[_stationInfoView addSubview:_collectView];
[_collectView addTarget:self action:@selector(collect)];
相关文章推荐
- BZOJ 2330 糖果
- Oracle 【to_number】【instr】
- DB2数据库的事务日志已满。. SQLCODE=-964, SQLSTATE=57011, DRIVER=3.63.108
- Android学习记录(十七)-背景图片缩放
- 【leetcode】217. Contains Duplicate
- find,vim的编程设置,SUID,SGID,SBIT,软.硬链接,重定向
- 九度 OJ 1057:众数
- Android 四大核心组件
- 【leetcode】171. Excel Sheet Column Number
- Android四大组件—ContentProvider
- Android覆盖安装,卸载移除广播监听器。未完待续
- 简单选择排序
- 多样化的Drawable
- 搜索引擎命令大全!
- webpack 入门
- flask实例项目发布到centos-nginx+uWSGI+flask
- Java数字证书对文件/加密/解密/签名/校验签名
- Multiple annotations found at this line: - Consider adding android:paddingEnd="10dp" to better supp
- UILable 设置多种字体颜色
- Jcrop+uploadify+php实现上传头像预览裁剪