您的位置:首页 > 其它

(按钮)上面为图片下部为文字的 按钮 控件

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)];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: