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,可令按钮在按下时发光。这可以用于信息按钮或者有些重要的按钮:
3344

被折叠的 条评论
为什么被折叠?



