由于项目需要,需要实现一个带图标的按钮,而自带的的 UIButton 并不能很好的满足需要,所以就自己实现一个,可以实现自定义标题位置,可以指定标题位置相对于图标的位置,支持上、下、左、右。
代码如下:
//
// Titleself.iconButton.swift
// Genyida
//
// Created by mac min on 2021/8/19.
// Copyright ? 2021 huangzhengguo. All rights reserved.
//
import Foundation
enum TitlePosition: Int8 {
case Top = 0
case Right = 1
case Bottom = 2
case Left = 3
}
class TitleButton: UIView {
var titleLabel = UILabel(frame: CGRect.zero)
var iconButton = UIButton(frame: CGRect.zero)
var clickCallback: ((Bool) -> Void)?
init(frame: CGRect, image: String, selectedImage: String, title: String, selectedTitle: String, selected: Bool, titlePosition: TitlePosition = .Bottom, textColor: UIColor = .white) {
super.init(frame: frame)
self.translatesAutoresizingMaskIntoConstraints = false
self.layer.cornerRadius = GlobalConstant.DEFAULT_VIEW_CORNER_RADIUS
self.backgroundColor = .clear
self.layer.borderWidth = 1.0
self.layer.borderColor = UIColor.clear.cgColor
self.isUserInteractionEnabled = true
let viewTap = UITapGestureRecognizer(target: self, action: #selector(viewTapAction))
self.addGestureRecognizer(viewTap)
self.titleLabel.translatesAutoresizingMaskIntoConstraints = false
self.titleLabel.textColor = textColor
self.iconButton.tag = 10001
self.iconButton.tintColor = .lightGray
self.iconButton.isUserInteractionEnabled = false
self.iconButton.translatesAutoresizingMaskIntoConstraints = false
self.iconButton.setImage(UIImage(named: image), for: .normal)
self.iconButton.setImage(UIImage(named: selectedImage), for: .selected)
self.addSubview(self.titleLabel)
self.addSubview(self.iconButton)
self.updateViews(selected: selected, title: title, selectedTitle: selectedTitle)
let labelLeading = NSLayoutConstraint(item: self.titleLabel, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1.0, constant: 0.0)
let labelTrailing = NSLayoutConstraint(item: self.titleLabel, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1.0, constant: 0.0)
let labelTop = NSLayoutConstraint(item: self.titleLabel, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 3.0)
let labelBottom = NSLayoutConstraint(item: self.titleLabel, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: 0.0)
let labelWidth = NSLayoutConstraint(item: self.titleLabel, attribute: .width, relatedBy: .equal, toItem: self, attribute: .width, multiplier: 0.5, constant: 0.0)
let labelHeight = NSLayoutConstraint(item: self.titleLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 21.0)
let buttonLeading = NSLayoutConstraint(item: self.iconButton, attribute: .leading, relatedBy: .equal, toItem: self.titleLabel, attribute: .trailing, multiplier: 1.0, constant: 3.0)
let buttonTrailing = NSLayoutConstraint(item: self.iconButton, attribute: .trailing, relatedBy: .equal, toItem: self.titleLabel, attribute: .leading, multiplier: 1.0, constant: -8.0)
var buttonTop = NSLayoutConstraint(item: self.iconButton, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 3.0)
var buttonBottom = NSLayoutConstraint(item: self.iconButton, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: -3.0)
let buttonWidthHeight = NSLayoutConstraint(item: self.iconButton, attribute: .width, relatedBy: .equal, toItem: self.iconButton, attribute: .height, multiplier: 1.0, constant: 0.0)
let buttonCenterX = NSLayoutConstraint(item: self.iconButton, attribute: .centerX, relatedBy: .equal, toItem: self.titleLabel, attribute: .centerX, multiplier: 1.0, constant: 0.0)
if titlePosition == .Top {
// 顶部标题
self.titleLabel.textAlignment = .center
buttonTop = NSLayoutConstraint(item: self.iconButton, attribute: .top, relatedBy: .equal, toItem: self.titleLabel, attribute: .bottom, multiplier: 1.0, constant: 3.0)
self.addConstraints([labelLeading, labelTop, labelTrailing, labelHeight])
self.addConstraints([buttonCenterX, buttonTop, buttonBottom, buttonWidthHeight])
} else if titlePosition == .Bottom {
// 底部标题
self.titleLabel.textAlignment = .center
buttonBottom = NSLayoutConstraint(item: self.iconButton, attribute: .bottom, relatedBy: .equal, toItem: self.titleLabel, attribute: .top, multiplier: 1.0, constant: -3.0)
self.addConstraints([labelLeading, labelBottom, labelTrailing, labelHeight])
self.addConstraints([buttonCenterX, buttonTop, buttonBottom, buttonWidthHeight])
} else if titlePosition == .Right {
// 右部标题
self.titleLabel.textAlignment = .left
self.addConstraints([labelTrailing, labelTop, labelBottom, labelWidth])
self.addConstraints([buttonTrailing, buttonTop, buttonBottom, buttonWidthHeight])
} else {
// 左部标题
self.titleLabel.textAlignment = .right
self.addConstraints([labelLeading, labelTop, labelBottom, labelWidth])
self.addConstraints([buttonLeading, buttonTop, buttonBottom, buttonWidthHeight])
}
}
func updateViews(selected: Bool, title: String, selectedTitle: String) -> Void {
if selected == true {
self.titleLabel.text = selectedTitle
} else {
self.titleLabel.text = title
}
self.iconButton.isSelected = selected
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc func viewTapAction(sender: UITapGestureRecognizer) -> Void {
let view = sender.view
let btn: UIButton = view?.viewWithTag(10001) as! UIButton
btn.isSelected = !btn.isSelected
self.clickCallback?(btn.isSelected)
}
}
|