<commit_before>//
//  RGInnerBadgeButton.swift
//  RGInnerBadgeButton
//
//  Created by <NAME> on 03/01/2017.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import UIKit

@IBDesignable
class RGInnerBadgeButton: UIButton {
    
    @IBInspectable
    var margin: CGFloat = 10 {
        didSet {
            setNeedsDisplay()
        }
    }
    
    @IBInspectable
    var countDiffSize: CGFloat = 4 {
        didSet {
            setNeedsDisplay()
        }
    }
    
    @IBInspectable
    var countInsetTop: CGFloat = 0 {
        didSet {
            setNeedsDisplay()
        }
    }
    
    @IBInspectable
    var countInsetLeft: CGFloat = 0 {
        didSet {
            setNeedsDisplay()
        }
    }
    
    @IBInspectable
    var countInsetBottom: CGFloat = 0 {
        didSet {
            setNeedsDisplay()
        }
    }
    
    @IBInspectable
    var countInsetRight: CGFloat = 0 {
        didSet {
            setNeedsDisplay()
        }
    }
    
    @IBInspectable
    var lineWidth: CGFloat = 1 / UIScreen.main.nativeScale {
        didSet {
            setNeedsDisplay()
        }
    }
    
    @IBInspectable
    var badgeNumber: Int = 0 {
        didSet {
            setNeedsDisplay()
        }
    }
    
    private var countEdgeInsets: UIEdgeInsets {
        return UIEdgeInsets(top: countInsetTop, left: countInsetLeft, bottom: countInsetBottom, right: countInsetRight)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        setNeedsDisplay()
    }
    
    override func draw(_ rect: CGRect) {
        if let titleLabel = titleLabel {
            let countFont = UIFont(name: titleLabel.font.fontName,
                                   size: titleLabel.font.pointSize - countDiffSize)!
            
            var countStringAttributes: [String: Any] = [:]
            countStringAttributes[NSFontAttributeName] = countFont
            if let textColor = titleLabel.textColor {
                countStringAttributes[NSForegroundColorAttributeName] = textColor.withAlphaComponent(titleLabel.alpha)
            } else {
                countStringAttributes[NSForegroundColorAttributeName] = UIColor.clear
            }
            let countString = NSAttributedString(string: String(badgeNumber), attributes: countStringAttributes)
            let badgeSize = CGSize(width: countString.size().width + countEdgeInsets.left + countEdgeInsets.right,
                                   height: countString.size().height + countEdgeInsets.top + countEdgeInsets.bottom)
            
            let inset = (badgeSize.width + margin) / 2
            titleEdgeInsets = UIEdgeInsetsMake(0, -inset, 0, inset)
            
            let countPoint = CGPoint(x: titleLabel.frame.maxX + margin + countEdgeInsets.left - inset + titleEdgeInsets.right,
                                     y: titleLabel.frame.midY - (countString.size().height / 2))
            
            countString.draw(at: countPoint)
            
            let badgeRect = CGRect(x: countPoint.x - countEdgeInsets.left,<commit_msg>hide badge when count zero<commit_after>//
//  RGInnerBadgeButton.swift
//  RGInnerBadgeButton
//
//  Created by <NAME> on 03/01/2017.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import UIKit

@IBDesignable
@objc class RGInnerBadgeButton: UIButton {
    
    @IBInspectable
    var margin: CGFloat = 10 {
        didSet {
            setNeedsDisplay()
        }
    }
    
    @IBInspectable
    var countDiffSize: CGFloat = 4 {
        didSet {
            setNeedsDisplay()
        }
    }
    
    @IBInspectable
    var countInsetTop: CGFloat = 0 {
        didSet {
            setNeedsDisplay()
        }
    }
    
    @IBInspectable
    var countInsetLeft: CGFloat = 0 {
        didSet {
            setNeedsDisplay()
        }
    }
    
    @IBInspectable
    var countInsetBottom: CGFloat = 0 {
        didSet {
            setNeedsDisplay()
        }
    }
    
    @IBInspectable
    var countInsetRight: CGFloat = 0 {
        didSet {
            setNeedsDisplay()
        }
    }
    
    @IBInspectable
    var lineWidth: CGFloat = 1 / UIScreen.main.nativeScale {
        didSet {
            setNeedsDisplay()
        }
    }
    
    @IBInspectable
    var badgeNumber: Int = 0 {
        didSet {
            setNeedsDisplay()
        }
    }
    
    private var countEdgeInsets: UIEdgeInsets {
        return UIEdgeInsets(top: countInsetTop, left: countInsetLeft, bottom: countInsetBottom, right: countInsetRight)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        setNeedsDisplay()
    }
    
    override func draw(_ rect: CGRect) {
        guard let titleLabel = titleLabel else {
            return
        }
        if badgeNumber == 0 {
            super.draw(rect)
        } else {
            let countFont = UIFont(name: titleLabel.font.fontName,
                                   size: titleLabel.font.pointSize - countDiffSize)!
            
            var countStringAttributes: [String: Any] = [:]
            countStringAttributes[NSFontAttributeName] = countFont
            if let textColor = titleLabel.textColor {
                countStringAttributes[NSForegroundColorAttributeName] = textColor.withAlphaComponent(titleLabel.alpha)
            } else {
                countStringAttributes[NSForegroundColorAttributeName] = UIColor.clear
            }
            let countString = NSAttributedString(string: String(badgeNumber), attributes: countStringAttributes)
            let badgeSize = CGSize(width: countString.size().width + countEdgeInsets.left + countEdgeInsets.right,
                                   height: countString.size().height + countEdgeInsets.top + countEdgeInsets.bottom)
            
            let inset = (badgeSize.width + margin) / 2
            titleEdgeInsets = UIEdgeInsetsMake(0, -inset, 0, inset)
            
            let countPoint = CGPoint(x: titleLabel.frame.maxX + margin + countEdgeInsets.left - inset + titleEdgeInsets.right,
                                     y: titleLabel.frame.midY - (countString.size().height / 2))
            
            countString.draw(at: countPoint)
            
            let badgeRect = CGRect(x: countPoint.x - countEdgeInsets.left,
{'fraction_non_alphanumeric': 0.08163265306122448, 'fraction_numerical': 0.008131377551020409, 'mean_word_length': 1.7452954048140044, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '249386', 'n_tokens_mistral': 1875, 'n_tokens_neox': 1783, 'n_words': 355}