您的位置:首页 > 移动开发 > Swift

swift 自定义UIButton 图片在上 文字在下

2016-05-13 07:54 441 查看
1 首先继承UIButton

2 实现imageRectForContentRect titleRectForContentRect

// 内部图片的frame

- (CGRect)imageRectForContentRect:(CGRect)contentRect

{

CGFloat imageW = contentRect.size.width;

CGFloat imageH = contentRect.size.height * IWTabBarButtonImageRatio;

return CGRectMake(0, 0, imageW, imageH);

}

// 内部文字的frame

- (CGRect)titleRectForContentRect:(CGRect)contentRect

{

CGFloat titleY = contentRect.size.height * IWTabBarButtonImageRatio;

CGFloat titleW = contentRect.size.width;

CGFloat titleH = contentRect.size.height - titleY;

return CGRectMake(0, titleY, titleW, titleH);

}

- (id)initWithFrame:(CGRect)frame

{

self = [super initWithFrame:frame];

if (self) {

// 图标居中

self.imageView.contentMode = UIViewContentModeCenter;

// 文字居中

self.titleLabel.textAlignment = NSTextAlignmentCenter;

}

return self;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: