Visual_Novel_iOS/crush/Crush/Src/Components/UI/BaseView/BadgeView.swift

99 lines
2.6 KiB
Swift
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// BadgeView.swift
// LegendTeam
//
// Created by on 16/12/21.
//
import UIKit
class BadgeView: UIView {
/// false
public var onlyShowPoint = false
/// 99 ... false
public var showMax99 = false
/// 0
public var badgeValue = 0 {
didSet {
reloadBadge()
}
}
/// 8
public var pointWidth: CGFloat = 8
///
public var badgeFont: UIFont = .t.tnmxs
private var leftPadding: CGFloat = 4
private var rightPadding: CGFloat = 4
private let defaultBgColor = UIColor.c.cin
private let defaultTextColor = UIColor.white
private let contentLabel = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
setupUI()
}
private func setupUI() {
isUserInteractionEnabled = false
backgroundColor = defaultBgColor
layer.masksToBounds = true
snp.makeConstraints { make in
make.width.greaterThanOrEqualTo(snp.height)
}
addSubview(contentLabel)
contentLabel.textColor = defaultTextColor
contentLabel.font = badgeFont
contentLabel.textAlignment = .center
contentLabel.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
}
private func reloadBadge() {
isHidden = false
contentLabel.isHidden = false
if badgeValue == 0 {
isHidden = true
return
}
if onlyShowPoint {
contentLabel.isHidden = true
contentLabel.snp.remakeConstraints { make in
make.edges.equalToSuperview()
make.size.equalTo(CGSize(width: pointWidth, height: pointWidth))
}
return
}
if showMax99 && badgeValue > 99 {
contentLabel.text = "···"
} else {
contentLabel.text = String(badgeValue)
}
contentLabel.snp.remakeConstraints { make in
make.leading.equalTo(self).offset(leftPadding)
make.trailing.equalTo(self).offset(-rightPadding)
make.width.greaterThanOrEqualTo(6)
make.top.bottom.equalTo(self)
}
}
override func layoutSubviews() {
super.layoutSubviews()
if bounds.size.width > 0 {
layer.cornerRadius = bounds.size.height * 0.5
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}