Swift图标标题按钮
一、自定义IconTitleButton类
import Foundation
/*
枚举 设置 图片的位置
*/
enum ButtonImagePosition : Int {
case imageTop = 0
case imageLeft
case imageBottom
case imageRight
}
extension UIButton {
/**
type :image 的位置
Space :图片文字之间的间距
*/
func setImagePosition(type:ButtonImagePosition,Space space:CGFloat) {
let imageWith :CGFloat = (imageView?.frame.size.width)!
let imageHeight :CGFloat = (imageView?.frame.size.height)!
var labelWidth :CGFloat = 0.0
var labelHeight :CGFloat = 0.0
labelWidth = CGFloat(titleLabel!.intrinsicContentSize.width)
labelHeight = CGFloat(titleLabel!.intrinsicContentSize.height)
var imageEdgeInsets :UIEdgeInsets = UIEdgeInsets()
var labelEdgeInsets :UIEdgeInsets = UIEdgeInsets()
switch type {
case .imageTop:
imageEdgeInsets = UIEdgeInsets.init(top: -labelHeight - space/2.0, left: 0, bottom: 0, right: -labelWidth)
labelEdgeInsets = UIEdgeInsets.init(top:0, left: -imageWith, bottom: -imageHeight-space/2.0, right: 0)
break;
case .imageLeft:
imageEdgeInsets = UIEdgeInsets.init(top:0, left:-space/2.0, bottom: 0, right:space/2.0)
labelEdgeInsets = UIEdgeInsets.init(top:0, left:space/2.0, bottom: 0, right: -space/2.0)
break;
case .imageBottom:
imageEdgeInsets = UIEdgeInsets.init(top:0, left:0, bottom: -labelHeight-space/2.0, right: -labelWidth)
labelEdgeInsets = UIEdgeInsets.init(top:-imageHeight-space/2.0, left:-imageWith, bottom: 0, right: 0)
break;
case .imageRight:
imageEdgeInsets = UIEdgeInsets.init(top:0, left:labelWidth+space/2.0, bottom: 0, right: -labelWidth-space/2.0)
labelEdgeInsets = UIEdgeInsets.init(top:0, left:-imageWith-space/2.0, bottom: 0, right:imageWith+space/2.0)
break;
}
self.titleEdgeInsets = labelEdgeInsets
self.imageEdgeInsets = imageEdgeInsets
}
}
二、使用
import UIKit
class IconTitleButtonController: BaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(btn1)
view.addSubview(btn2)
view.addSubview(btn3)
view.addSubview(btn4)
}
lazy var btn1:UIButton = {
let btn = UIButton.init(frame: CGRect.init(x: 50, y: 100, width: 120, height: 40))
btn.backgroundColor = .gray
btn.setImage(UIImage.init(named: "tabbar_2a"), for: .normal)
btn.setTitle("测试标题", for: .normal)
btn.setImagePosition(type: .imageLeft, Space: 5)
return btn
}()
lazy var btn2:UIButton = {
let btn = UIButton.init(frame: CGRect.init(x: 50, y: 160, width: 120, height: 40))
btn.backgroundColor = .gray
btn.setImage(UIImage.init(named: "tabbar_2a"), for: .normal)
btn.setTitle("测试标题", for: .normal)![]()
btn.setImagePosition(type: .imageRight, Space: 5)
return btn
}()
lazy var btn3:UIButton = {
let btn = UIButton.init(frame: CGRect.init(x: 50, y:220, width: 120, height: 80))
btn.backgroundColor = .gray
btn.setImage(UIImage.init(named: "tabbar_2a"), for: .normal)
btn.setTitle("测试标题", for: .normal)
btn.setImagePosition(type: .imageTop, Space: 10)
return btn
}()
lazy var btn4:UIButton = {
let btn = UIButton.init(frame: CGRect.init(x: 50, y: 320, width: 120, height: 80))
btn.backgroundColor = .gray
btn.setImage(UIImage.init(named: "tabbar_2a"), for: .normal)
btn.setTitle("测试标题", for: .normal)
btn.setImagePosition(type: .imageBottom, Space: 10)
return btn
}()
}
三、效果图