|
import UIKit |
|
|
|
@IBDesignable |
|
class BadgeButton: UIButton { |
|
|
|
// MARK: IBInspectable |
|
|
|
@IBInspectable var cornerRadius: CGFloat = 8 { |
|
didSet { |
|
layer.cornerRadius = cornerRadius |
|
} |
|
} |
|
@IBInspectable var badgeSize: CGFloat = 28 |
|
@IBInspectable var badgeColor: UIColor = .blue |
|
@IBInspectable var badgeLabelColor: UIColor = .white |
|
|
|
// MARK: Properties |
|
|
|
private let badgeView = UIView() |
|
private let badgeLabel = UILabel() |
|
|
|
override var isEnabled: Bool { |
|
didSet { |
|
alpha = isEnabled ? 1 : 0.4 |
|
} |
|
} |
|
|
|
// MARK: Initializer |
|
|
|
override init(frame: CGRect) { |
|
super.init(frame: frame) |
|
commonInit() |
|
} |
|
|
|
required init?(coder aDecoder: NSCoder) { |
|
super.init(coder: aDecoder) |
|
} |
|
|
|
override func awakeFromNib() { |
|
super.awakeFromNib() |
|
commonInit() |
|
} |
|
|
|
override func prepareForInterfaceBuilder() { |
|
super.prepareForInterfaceBuilder() |
|
commonInit() |
|
|
|
if let title = title(for: state) { |
|
setAttributedTitle(NSAttributedString(string: title, attributes: [NSAttributedString.Key(String(kCTLanguageAttributeName)): "ja"]), for: .normal) |
|
} |
|
} |
|
|
|
private func commonInit() { |
|
// - View |
|
isExclusiveTouch = true |
|
clipsToBounds = false |
|
// - Layer |
|
layer.cornerRadius = cornerRadius |
|
|
|
setupViews() |
|
} |
|
|
|
private func setupViews() { |
|
// - Badge View |
|
badgeView.backgroundColor = badgeColor |
|
badgeView.alpha = 0 |
|
badgeView.isHidden = true |
|
badgeView.layer.cornerRadius = badgeSize / 2 |
|
badgeView.isUserInteractionEnabled = false |
|
badgeView.translatesAutoresizingMaskIntoConstraints = false |
|
addSubview(badgeView) |
|
|
|
NSLayoutConstraint.activate([ |
|
badgeView.topAnchor.constraint(equalTo: topAnchor, constant: -(badgeSize / 2)), |
|
badgeView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: badgeSize / 2), |
|
badgeView.widthAnchor.constraint(equalToConstant: badgeSize), |
|
badgeView.heightAnchor.constraint(equalToConstant: badgeSize) |
|
]) |
|
// - Badge Label |
|
badgeLabel.textColor = badgeLabelColor |
|
badgeLabel.backgroundColor = .clear |
|
badgeLabel.font = .systemFont(ofSize: 12, weight: .bold) |
|
badgeLabel.numberOfLines = 1 |
|
badgeLabel.textAlignment = .center |
|
badgeLabel.translatesAutoresizingMaskIntoConstraints = false |
|
badgeView.addSubview(badgeLabel) |
|
|
|
NSLayoutConstraint.activate([ |
|
badgeLabel.topAnchor.constraint(equalTo: badgeView.topAnchor, constant: 0), |
|
badgeLabel.leadingAnchor.constraint(equalTo: badgeView.leadingAnchor, constant: 0), |
|
badgeLabel.trailingAnchor.constraint(equalTo: badgeView.trailingAnchor, constant: 0), |
|
badgeLabel.bottomAnchor.constraint(equalTo: badgeView.bottomAnchor, constant: 0) |
|
]) |
|
} |
|
|
|
func setCount(_ badgeCount: Int) { |
|
badgeLabel.text = badgeCount > 99 ? "99+" : "\(badgeCount)" |
|
UIView.animate(withDuration: 0.3, animations: { |
|
self.badgeView.alpha = 0 >= badgeCount ? 0 : 1 |
|
self.badgeView.isHidden = 0 >= badgeCount |
|
}) |
|
} |
|
} |