Files
OrderScheduling/Pods/ESTabBarController-swift/Sources/ESTabBarItemBadgeView.swift
DDIsFriend f0e8a1709d initial
2023-08-18 17:28:57 +08:00

117 lines
4.5 KiB
Swift
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.

//
// ESTabBarItemBadgeView.swift
//
// Created by Vincent Li on 2017/2/8.
// Copyright (c) 2013-2018 ESTabBarController (https://github.com/eggswift/ESTabBarController)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import UIKit
/*
* ESTabBarItemBadgeView
* item使badgeESTabBarItemBadgeView
* ESTabBarItemContentViewbadgeViewESTabBarItemBadgeView
*/
open class ESTabBarItemBadgeView: UIView {
///
public static var defaultBadgeColor = UIColor(red: 255.0/255.0, green: 59.0/255.0, blue: 48.0/255.0, alpha: 1.0)
/// Badge color
open var badgeColor: UIColor? = defaultBadgeColor {
didSet {
imageView.backgroundColor = badgeColor
}
}
/// Badge value, supprot nil, "", "1", "someText". Hidden when nil. Show Little dot style when "".
open var badgeValue: String? {
didSet {
badgeLabel.text = badgeValue
}
}
/// Image view
open var imageView: UIImageView = {
let imageView = UIImageView.init(frame: CGRect.zero)
imageView.backgroundColor = .clear
return imageView
}()
/// badgeValueLabel
open var badgeLabel: UILabel = {
let badgeLabel = UILabel.init(frame: CGRect.zero)
badgeLabel.backgroundColor = .clear
badgeLabel.textColor = .white
badgeLabel.font = UIFont.systemFont(ofSize: 13.0)
badgeLabel.textAlignment = .center
return badgeLabel
}()
/// Initializer
public override init(frame: CGRect) {
super.init(frame: frame)
self.addSubview(imageView)
self.addSubview(badgeLabel)
self.imageView.backgroundColor = badgeColor
}
public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
/*
* layoutSubviews()
**/
open override func layoutSubviews() {
super.layoutSubviews()
guard let badgeValue = badgeValue else {
imageView.isHidden = true
badgeLabel.isHidden = true
return
}
imageView.isHidden = false
badgeLabel.isHidden = false
if badgeValue == "" {
imageView.frame = CGRect.init(origin: CGPoint.init(x: (bounds.size.width - 8.0) / 2.0, y: (bounds.size.height - 8.0) / 2.0), size: CGSize.init(width: 8.0, height: 8.0))
} else {
imageView.frame = bounds
}
imageView.layer.cornerRadius = imageView.bounds.size.height / 2.0
badgeLabel.sizeToFit()
badgeLabel.center = imageView.center
}
/*
* badgeframebadge
* badgeContentContentbadgeOffset
*/
open override func sizeThatFits(_ size: CGSize) -> CGSize {
guard let _ = badgeValue else {
return CGSize.init(width: 18.0, height: 18.0)
}
let textSize = badgeLabel.sizeThatFits(CGSize.init(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude))
return CGSize.init(width: max(18.0, textSize.width + 10.0), height: 18.0)
}
}