重写UIButton属性

UIButton  里面有默认的 image 和 title 属性  可以新建一个类继承UIButton 。  在新类里 重写 image 和title 两个的属性 可以在按钮上设置图片和标题  

这两个方法分别是 


- (CGRect)imageRectForContentRect:(CGRect)contentRect

{

//  里面的数据 根据 自己的需求 来算

    CGFloat imageW = contentRect.size.width-50;

    CGFloat imageH = contentRect.size.height * CustomBtnImage-15;

    return CGRectMake(23, 15, imageW, imageH);

}


- (CGRect)titleRectForContentRect:(CGRect)contentRect

{

    CGFloat titleY = contentRect.size.height * CustomBtnImage-10;

    CGFloat titleW = contentRect.size.width;

    CGFloat titleH = contentRect.size.height - titleY;

    return CGRectMake(-2, titleY, titleW, titleH);

}


通过代码 创建的Button  

Button *btn =[Button buttonWithType:UIButtonTypeCustom];

    btn.frame = CGRectMake(0, 0, 74, 75);

    [btn setTitle:@"首页" forState:UIControlStateNormal];

    [btn setImage:[UIImage imageNamed:@"menu_icon.png"] forState:UIControlStateNormal];

    [btn setBackgroundColor:[UIColor clearColor]];

    btn.titleLabel.textAlignment = NSTextAlignmentCenter;

    btn.titleLabel.font = [UIFont systemFontOfSize:14];

    // 为Button 添加动作 Action

//    [btn addTarget:self action:@selector(testMe) forControlEvents:UIControlEventTouchUpInside];


   btn.showsTouchWhenHighlighted = YES;    

    //  这个属性设置为YES,可令按钮在按下时发光。这可以用于信息按钮或者有些重要的按钮:



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值