一、定义一个枚举
enum ButtonImageAndTitlePossitionStyle {
case systemDefault //图片在左,文字在右,整体居中。(系统,没有间距设置)
case imageIsLeft //图片在左,文字在右,整体居中
case imageIsRight //图片在右,文字在左,整体居中
case imageIsTop //图片在上,文字在下,整体居中
case imgageIsBottom //图片在下,文字在上,整体居中
}
二、UIbutton的 extension
extension UIButton {
//MARK: - 按枚举将 btn 的 image 和 title 之间位置处理
func setupButtonImageAndTitlePossitionWith(padding: CGFloat, style: ButtonImageAndTitlePossitionStyle){
let imageRect: CGRect = self.imageView?.frame ?? CGRect.init()
let titleRect: CGRect = self.titleLabel?.frame ?? CGRect.init()
let selfWidth: CGFloat = self.frame.size.width
let selfHeight: CGFloat = self.frame.size.height
let totalHeight = titleRect.size.height + padding + imageRect.size.height
switch style {
case .imageIsLeft:
self.titleEdgeInsets = UIEdgeInsets(top: 0, left: padding / 2, bottom: 0, right: -padding / 2)
self.imageEdgeInsets = UIEdgeInsets(top: 0, left: -padding / 2, bottom: 0, right: padding / 2)
case .imageIsRight:
self.titleEdgeInsets = UIEdgeInsets(top: 0, left: -(imageRect.size.width + padding/2), bottom: 0, right: (imageRect.size.width + padding/2))
self.imageEdgeInsets = UIEdgeInsets(top: 0, left: (titleRect.size.width + padding / 2), bottom: 0, right: -(titleRect.size.width + padding/2))
case .imageIsTop :
self.titleEdgeInsets = UIEdgeInsets(top: ((selfHeight - totalHeight) / 2 + imageRect.size.height + padding - titleRect.origin.y), left: (selfWidth / 2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2, bottom: -((selfHeight - totalHeight) / 2 + imageRect.size.height + padding - titleRect.origin.y), right: -(selfWidth / 2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2)
self.imageEdgeInsets = UIEdgeInsets(top: ((selfHeight - totalHeight) / 2 - imageRect.origin.y), left: (selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2), bottom: -((selfHeight - totalHeight) / 2 - imageRect.origin.y), right: -(selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2))
case .imgageIsBottom:
self.titleEdgeInsets = UIEdgeInsets(top: ((selfHeight - totalHeight) / 2 - titleRect.origin.y), left: (selfWidth / 2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2, bottom: -((selfHeight - totalHeight) / 2 - titleRect.origin.y), right: -(selfWidth/2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2)
self.imageEdgeInsets = UIEdgeInsets(top: ((selfHeight - totalHeight) / 2 + titleRect.size.height + padding - imageRect.origin.y), left: (selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2), bottom: -((selfHeight - totalHeight) / 2 + titleRect.size.height + padding - imageRect.origin.y), right: -(selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2))
default:
self.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
self.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
}
}
三、使用
- 注意啦,使用前需保证 UIButton的frame已经完成设置
- 如下使用
btn.setupButtonImageAndTitlePossitionWith(padding: 5, style: .imageIsRight)
网友评论