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;
}
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;
}
相关文章推荐
- Swift中如何修改函数的参数
- 对照Java学习Swift--嵌套类型(Nested Types)
- Swift的类型推断会不会造成类型不确定的困扰?
- 3.swift修饰符
- error in Swift. “Consecutive Declarations On A Line Must Be Separated By ';'”
- 2.Swift - 访问控制(private,internal,public)
- Swift之自动闭包(@autoclosure、@noescape、@escape、??)
- Swift-闭包
- [swift学习之十]扩展语法练习
- 1.swift学习之路---知识点积累
- 【Swift学习】Swift编程之旅---可选链(二十一)
- 对照Java学习Swift--类型转换(Type Casting)
- Swift:闭包
- Swift设计模式之备忘录模式
- Swift设计模式之策略模式
- Swift设计模式之抽象工厂模式
- Swift设计模式之创建者模式
- Swift设计模式之单例模式
- Swift设计模式之迭代器模式
- Swift设计模式之访问者模式