![这里写代码片](http://img.blog.csdn.net/20160616112611617)
//用UIButton来实现上图中样式
下面是自定义的Button:
#import <UIKit/UIKit.h>
@interface cusButton : UIButton
@end
#import "cusButton.h"
@interface cusButton()
{
CGRect boundingRect;
}
@end
@implementation cusButton
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
self.titleLabel.textAlignment = NSTextAlignmentCenter;
[self.titleLabel setFont:[UIFont boldSystemFontOfSize:13.0]];
if (self.tag == 0) {
[self setTitle:@"首页" forState:UIControlStateNormal];
[self setImage:[UIImage imageNamed:@"tabbar0"] forState:UIControlStateNormal];
}else if (self.tag == 1){
[self setTitle:@"工作流" forState:UIControlStateNormal];
[self setImage:[UIImage imageNamed:@"tabbar1"] forState:UIControlStateNormal];
}
boundingRect=[self.titleLabel.text boundingRectWithSize:CGSizeMake(320,13.0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13.0]} context:nil];
}
//自定义的初始化方法
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
}
return self;
}
//1.重写方法,改变 图片的位置 在 titleRect..方法后执行
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
CGFloat width=24;
CGFloat height=24;
CGFloat imageX=(self.frame.size.width-width)/2;
return CGRectMake(imageX, 5, width, height);
}
//2.改变title文字的位置,构造title的矩形即可
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
CGFloat width=80;
CGFloat height=25;
CGFloat imageX=(self.frame.size.width-width)/2;
CGFloat imageY=contentRect.origin.y+boundingRect.size.height+10;
return CGRectMake(imageX, imageY, width, height);
}
使用:
cusButton *btn = [[cusButton alloc]initWithFrame:CGRectMake(x, 0,with,myview.frame.size.height)];
实现button的Image在titleLabel的上面
最新推荐文章于 2021-10-14 11:41:06 发布