在开发中我们常遇到要修改按钮图文位置,那么在swift中我们应该怎么做比较好呢?
对按钮做些扩展,初始化完后调用方法就可以直接修改图文位置
首先需要设置枚举,确定图文位置
enum XButtonEdgeInsetsStyle {
case ImageTop //图片在上,文字在下
case ImageLeft //图片在上,文字在下
case ImageBottom //图片在上,文字在下
case ImageRight //图片在上,文字在下
}
之后设置图文位置
/**
># Important:按钮图文位置设置
知识点:titleEdgeInsets是title相对于其上下左右的inset,跟tableView的contentInset是类似的,
* 如果只有title,那它上下左右都是相对于button的,image也是一样;
* 如果同时有image和label,那这时候image的上左下是相对于button,右边是相对于label的;title的上右下是相对于button,左边是相对于image的。
*/
func layoutButtonWithEdgInsetStyle(_ style: XButtonEdgeInsetsStyle,_ space:CGFloat){
//获取image宽高
let imageW = self.imageView?.frame.size.width
let imageH = self.imageView?.frame.size.height
//获取label宽高
var lableW = self.titleLabel?.intrinsicContentSize.width
let lableH = self.titleLabel?.intrinsicContentSize.height
var imageEdgeInsets:UIEdgeInsets = .zero
var lableEdgeInsets:UIEdgeInsets = .zero
if self.frame.size.width <= lableW! { //如果按钮文字超出按钮大小,文字宽为按钮大小
lableW = self.frame.size.width
}
//根据传入的 style 及 space 确定 imageEdgeInsets和labelEdgeInsets的值
switch style {
case .ImageTop:
imageEdgeInsets = UIEdgeInsetsMake(0.0 - lableH! - space/2.0, 0, 0, 0.0 - lableW!)
lableEdgeInsets = UIEdgeInsetsMake(0, 0.0 - imageW!, 0.0 - imageH! - space/2.0, 0)
case .ImageLeft:
imageEdgeInsets = UIEdgeInsetsMake(0, 0.0 - space/2.0, 0, space/2.0)
lableEdgeInsets = UIEdgeInsetsMake(0, space/2.0, 0, 0.0 - space/2.0)
case .ImageBottom:
imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0.0 - lableH! - space/2.0, 0.0 - lableW!)
lableEdgeInsets = UIEdgeInsetsMake(0.0 - imageH! - space/2.0, 0.0 - imageW!, 0, 0)
case .ImageRight:
imageEdgeInsets = UIEdgeInsetsMake(0, lableW! + space/2.0, 0, 0.0 - lableW! - space/2.0)
lableEdgeInsets = UIEdgeInsetsMake(0, 0.0 - imageW! - space/2.0, 0, imageW! + space/2.0)
}
//赋值
self.titleEdgeInsets = lableEdgeInsets
self.imageEdgeInsets = imageEdgeInsets
}
调用起来很方便,初始化后
button.layoutButtonWithEdgInsetStyle(.ImageTop, 10)