class GoodsLabelsView: UIView {
/// 标签底层View
var markView: UIView!
/// 清楚标签按钮
var clearMarkButton: UIButton!
var totalHeight : CGFloat = 0
var totalWidth : CGFloat = 0
var selectedBtn: UIButton?
var buttonTagBlock: ((_ tag: Int) -> Void)?
override init(frame: CGRect) {
super.init(frame: frame)
// 标签底层View
markView = UIView()
addSubview(markView)
markView.snp.makeConstraints { make in
make.top.equalTo(self)
make.left.equalTo(self)
make.right.equalTo(self)
make.height.equalTo(200)
}
}
func drawLabels(commRanges: [commRangesModel]?) -> CGFloat {
guard let commranges = commRanges else {
return 0
}
// 绘制实时标签
var tempTotalWidth: CGFloat = 0
for i in 0 ..< commranges.count {
let button = UIButton(type: .custom)
button.setTitle(commranges[i].specification, for: .normal)
button.titleLabel?.font = UIFont.systemFont(ofSize: 12)
button.setTitleColor(UIColor(hexString: "#292929"), for: .normal)
button.setTitleColor(UIColor(hexString: "#df3523"), for: .selected)
button.setBackgroundImage(UIImage.image(color: UIColor(hexString: "#f5f5f5")!), for: .normal)
button.setBackgroundImage(UIImage.image(color: UIColor(hexString: "#fef2f1")!), for: .selected)
button.layer.cornerRadius = 4
button.layer.masksToBounds = true
button.layer.borderColor = UIColor(hexString: "#f5f5f5")?.cgColor
button.layer.borderWidth = 1
markView.addSubview(button)
button.addTarget(self, action: #selector(buttonClick(btn:)), for: .touchUpInside)
button.tag = i
if i == 0 {
button.isSelected = true
button.layer.borderColor = UIColor(hexString: "#df3523")?.cgColor
self.selectedBtn = button
}
let markWidth = self.getLabelWidth(commranges[i].specification?.count ?? 0) + 10
tempTotalWidth += markWidth
if tempTotalWidth > (ScreenSize.SCREEN_WIDTH - 30) {
tempTotalWidth = markWidth
self.totalHeight += 30
self.totalWidth = 0
}
button.snp.makeConstraints { make in
make.top.equalTo(markView).offset(self.totalHeight)
make.left.equalTo(markView).offset(self.totalWidth)
make.width.equalTo(self.getLabelWidth(commranges[i].specification?.count ?? 0))
make.height.equalTo(20)
}
// 累加每行的总长度
self.totalWidth += markWidth
}
// 更新高度
markView.snp.updateConstraints { make in
make.height.equalTo(self.totalHeight+20)
}
return self.totalHeight
}
func changeButtonSelect(superview:UIView, num: Int) {
for i in superview.subviews {
guard let btn = i as? UIButton else {
return
}
if btn.tag == num {
btn.isSelected = true
btn.layer.borderColor = UIColor(hexString: "#df3523")?.cgColor
self.selectedBtn = btn
}else{
btn.isSelected = false
btn.layer.borderColor = UIColor(hexString: "#f5f5f5")?.cgColor
}
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func getLabelWidth(_ count: Int) -> CGFloat {
return CGFloat(count * 10 + 20)
}
@objc func buttonClick(btn: UIButton) {
if btn != self.selectedBtn {
btn.isSelected = true
btn.layer.borderColor = UIColor(hexString: "#df3523")?.cgColor
self.selectedBtn?.isSelected = false
self.selectedBtn?.layer.borderColor = UIColor(hexString: "#f5f5f5")?.cgColor
self.selectedBtn = btn
self.buttonTagBlock?(btn.tag)
}else{
return
}
}
}
网友评论