<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">我们都知道UIButton上有titleLable和imageView,但是有时候我们不想用它默认的位置的,想要改变他的文字和图片的位置,那么如何来达到效果呢?</span>
简单思路就是:新建一个类继承于UIButton,重写两个函数,
-(CGRect) imageRectForContentRect:(CGRect)contentRect ; 设置图片的位置
-(CGRect) titleRectForContentRect:(CGRect)contentRect; 设置文字的位置
这样就可以随意设置button上的文字、图片的位置和大小了。
//自定义button的.m文件
//重写父类方法设置按钮图片和文字的位置
-(CGRect)imageRectForContentRect:(CGRect)contentRect{
return CGRectMake(self.bounds.size.width*7/8, 10, self.bounds.size.width/8 , self.bounds.size.height/2);
}
-(CGRect)titleRectForContentRect:(CGRect)contentRect{
return CGRectMake(10, 0, self.bounds.size.width/2, self.bounds.size.height);
}
<