swift 适用于iOS的徽章标签

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift 适用于iOS的徽章标签相关的知识,希望对你有一定的参考价值。

import UIKit

/**
 * [Badge.swift](https://gist.github.com/yonat/75a0f432d791165b1fd6) をベース
 */
class BadgeLabel: UILabel {
    var badgeColor: UIColor = UIColor.redColor()

    convenience init(badge: String?, color: UIColor = UIColor.redColor()) {
        self.init()
        badgeColor = color
        text = badge
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        initialize()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        initialize()
    }

    func initialize() {
        textColor = UIColor.whiteColor()
        backgroundColor = UIColor.clearColor()
        textAlignment = .Center
    }

    override func drawRect(rect: CGRect) {
        let context = UIGraphicsGetCurrentContext()
        CGContextSetFillColorWithColor(context, badgeColor.CGColor)
        CGContextFillEllipseInRect(context, rect)
        super.drawRect(rect)
    }

    override var text: String? {
        didSet {
            guard let text = self.text else {
                hidden = true
                return
            }
            if text.isEmpty {
                hidden = true
                return
            }
            if let count = Int(text) where count == 0 {
                hidden = true
                return
            }
            hidden = false
        }
    }

    let margin: CGFloat = 3

    override func drawTextInRect(rect: CGRect) {
        let insets = UIEdgeInsets(top: margin, left: margin, bottom: margin, right: margin)
        let rect = UIEdgeInsetsInsetRect(rect, insets)
        super.drawTextInRect(rect)
    }

    override func intrinsicContentSize() -> CGSize {
        var size = super.intrinsicContentSize()
        let width = size.width + margin * 2
        let height = size.height + margin * 2
        if width >= height {
            size.width = width
            size.height = width
        } else {
            size.width = height
            size.height = height
        }
        return size
    }
}

extension UIBarButtonItem {
    convenience init(image: UIImage?, badge: String?, target: AnyObject?, action: Selector) {
        let button = UIButton(type: .Custom)
        button.frame = CGRect(origin: CGPointZero, size: image?.size ?? CGSizeZero)
        button.setImage(image, forState: .Normal)
        button.addTarget(target, action: action, forControlEvents: .TouchUpInside)
        self.init(customView: button)

        let label = BadgeLabel(badge: badge)
        label.tag = self.dynamicType.badgeTag
        button.addSubview(label)

        label.translatesAutoresizingMaskIntoConstraints = false
        button.addConstraint(NSLayoutConstraint(item: label, attribute: .CenterX, relatedBy: .Equal, toItem: button, attribute: .CenterX, multiplier: 1, constant: 10))
        button.addConstraint(NSLayoutConstraint(item: button, attribute: .CenterY, relatedBy: .Equal, toItem: label, attribute: .CenterY, multiplier: 1, constant: 10))
    }

    var badgeButton: UIButton {
        return self.customView as! UIButton
    }

    var badgeLabel: BadgeLabel {
        return self.customView?.viewWithTag(self.dynamicType.badgeTag) as! BadgeLabel
    }

    var badgeText: String? {
        get { return badgeLabel.text }
        set {
            badgeLabel.text = newValue
            badgeLabel.layoutIfNeeded()
        }
    }

    private static let badgeTag = 8181
}

以上是关于swift 适用于iOS的徽章标签的主要内容,如果未能解决你的问题,请参考以下文章

Swift 徽章通知在 iOS 10 之后贬值

在 iOS 上重置通知徽章的问题 - Swift [重复]

iOS 7 UITabBar 徽章位置

iOS 7.1 中的 UITabBarItem 更改徽章颜色

是否可以仅显示 Firebase 推送通知的徽章,而在 IOS(Swift 或 Obj-C)中没有警报?

获取数据后更新选项卡项徽章(Swift)