iOS 自定义UIButton的title和image位置

来源:互联网 发布:站长之家源码 编辑:程序博客网 时间:2024/05/29 13:22

做项目需要,所以找了方法,与大家分享~ 亲测可用,contentRect就是我们给UIButton设置的frame值~

张洋给的自定义UIButton的title和image位置的方法:

1.继承自UIButton

2.重写这两个方法:

-(CGRect)titleRectForContentRect:(CGRect)contentRect{    CGFloat titleY = contentRect.size.height *0.6;    CGFloat titleW = contentRect.size.width;    CGFloat titleH = contentRect.size.height - titleY;    return CGRectMake(0, titleY, titleW, titleH);}-(CGRect)imageRectForContentRect:(CGRect)contentRect{    CGFloat imageW = CGRectGetWidth(contentRect);     CGFloat imageH = contentRect.size.height * 0.6;     return CGRectMake(0, 0, imageW, imageH);}

0 0
原创粉丝点击