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


有的时候在做一些基础控件的时候系统的不能满足需要,就需要自己自定义一些控件了,这个按钮是上面显示图片,下半部分显示文字.


#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 allocinit];

        [self.topBtn setImage:[UIImage imageNamed:@"collect"]forState:UIControlStateNormal];

        [self.topBtn setAdjustsImageWhenHighlighted:NO];

        

        [self addSubview:self.topBtn];


        // 添加Label

        self.titleLabel=[[UILabel allocinit];

        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(00, W * 0.330);

    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 allocinit];

}


-(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)];


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值