Skip to content

Instantly share code, notes, and snippets.

@yonat
Last active December 22, 2022 09:40
Show Gist options
  • Select an option

  • Save yonat/75a0f432d791165b1fd6 to your computer and use it in GitHub Desktop.

Select an option

Save yonat/75a0f432d791165b1fd6 to your computer and use it in GitHub Desktop.

Revisions

  1. yonat revised this gist Oct 19, 2018. 1 changed file with 2 additions and 7 deletions.
    9 changes: 2 additions & 7 deletions Badge.swift
    Original file line number Diff line number Diff line change
    @@ -60,13 +60,8 @@ extension UIButton {

    /// regular `setTitle()` fails after using `setAttributedTitle()` (which called if you changed `titleSize`)
    public func changeTitleButKeepAttributes(_ newTitle: String) {
    if let attributedTitle = attributedTitle(for: .normal) {
    let attributedDate = NSMutableAttributedString(attributedString: attributedTitle)
    attributedDate.replaceCharacters(in: NSRange(location: 0, length: attributedDate.length), with: newTitle)
    setAttributedTitle(attributedDate, for: .normal)
    } else {
    setTitle(newTitle, for: .normal)
    }
    setAttributedTitle(nil, for: .normal)
    setTitle(newTitle, for: .normal)
    }

    func roundWithTitleSize(_ size: CGFloat) {
  2. yonat revised this gist Oct 19, 2018. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion Badge.swift
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,8 @@ import UIKit
    public extension UILabel {
    public convenience init(badgeText: String, color: UIColor = .red, fontSize: CGFloat = UIFont.smallSystemFontSize) {
    self.init()
    text = " \(badgeText) "
    text = badgeText.count > 1 ? " \(badgeText) " : badgeText
    textAlignment = .center
    textColor = .white
    backgroundColor = color

  3. yonat revised this gist Oct 17, 2018. 1 changed file with 49 additions and 41 deletions.
    90 changes: 49 additions & 41 deletions Badge.swift
    Original file line number Diff line number Diff line change
    @@ -13,45 +13,42 @@

    import UIKit

    extension UILabel {
    convenience init(badgeText: String, color: UIColor = UIColor.redColor(), fontSize: CGFloat = UIFont.smallSystemFontSize()) {
    public extension UILabel {
    public convenience init(badgeText: String, color: UIColor = .red, fontSize: CGFloat = UIFont.smallSystemFontSize) {
    self.init()
    text = " \(badgeText) "
    textColor = UIColor.whiteColor()
    textColor = .white
    backgroundColor = color

    font = UIFont.systemFontOfSize(fontSize)
    font = UIFont.systemFont(ofSize: fontSize)
    layer.cornerRadius = fontSize * CGFloat(0.6)
    clipsToBounds = true

    translatesAutoresizingMaskIntoConstraints = false
    addConstraint(NSLayoutConstraint(item: self, attribute: .Width, relatedBy: .GreaterThanOrEqual, toItem: self, attribute: .Height, multiplier: 1, constant: 0))
    addConstraint(NSLayoutConstraint(item: self, attribute: .width, relatedBy: .greaterThanOrEqual, toItem: self, attribute: .height, multiplier: 1, constant: 0))
    }
    }

    extension UIButton {
    /// show background as rounded rect, like mail addressees
    var rounded: Bool {
    public var rounded: Bool {
    get { return layer.cornerRadius > 0 }
    set { roundWithTitleSize(newValue ? titleSize : 0) }
    }

    /// removes other title attributes
    var titleSize: CGFloat {
    public var titleSize: CGFloat {
    get {
    let titleFont = attributedTitleForState(.Normal)?.attribute(NSFontAttributeName, atIndex: 0, effectiveRange: nil) as? UIFont
    return titleFont?.pointSize ?? UIFont.buttonFontSize()
    let titleFont = attributedTitle(for: .normal)?.attribute(.font, at: 0, effectiveRange: nil) as? UIFont
    return titleFont?.pointSize ?? UIFont.buttonFontSize
    }
    set {
    // TODO: use current attributedTitleForState(.Normal) if defined
    if UIFont.buttonFontSize() == newValue || 0 == newValue {
    setTitle(currentTitle, forState: .Normal)
    }
    else {
    if UIFont.buttonFontSize == newValue || 0 == newValue {
    setTitle(currentTitle, for: .normal)
    } else {
    let attrTitle = NSAttributedString(string: currentTitle ?? "", attributes:
    [NSFontAttributeName: UIFont.systemFontOfSize(newValue), NSForegroundColorAttributeName: currentTitleColor]
    )
    setAttributedTitle(attrTitle, forState: .Normal)
    [.font: UIFont.systemFont(ofSize: newValue), .foregroundColor: currentTitleColor])
    setAttributedTitle(attrTitle, for: .normal)
    }

    if rounded {
    @@ -60,49 +57,60 @@ extension UIButton {
    }
    }

    func roundWithTitleSize(size: CGFloat) {
    /// regular `setTitle()` fails after using `setAttributedTitle()` (which called if you changed `titleSize`)
    public func changeTitleButKeepAttributes(_ newTitle: String) {
    if let attributedTitle = attributedTitle(for: .normal) {
    let attributedDate = NSMutableAttributedString(attributedString: attributedTitle)
    attributedDate.replaceCharacters(in: NSRange(location: 0, length: attributedDate.length), with: newTitle)
    setAttributedTitle(attributedDate, for: .normal)
    } else {
    setTitle(newTitle, for: .normal)
    }
    }

    func roundWithTitleSize(_ size: CGFloat) {
    let padding = size / 4
    layer.cornerRadius = padding + size * 1.2 / 2
    let sidePadding = padding * 1.5
    contentEdgeInsets = UIEdgeInsets(top: padding, left: sidePadding, bottom: padding, right: sidePadding)

    if size.isZero {
    backgroundColor = UIColor.clearColor()
    setTitleColor(tintColor, forState: .Normal)
    }
    else {
    backgroundColor = .clear
    setTitleColor(tintColor, for: .normal)
    } else {
    backgroundColor = tintColor
    let currentTitleColor = titleColorForState(.Normal)
    let currentTitleColor = titleColor(for: .normal)
    if currentTitleColor == nil || currentTitleColor == tintColor {
    setTitleColor(UIColor.whiteColor(), forState: .Normal)
    setTitleColor(.white, for: .normal)
    }
    }
    }

    override public func tintColorDidChange() {
    // swiftlint:disable override_in_extension
    open override func tintColorDidChange() {
    super.tintColorDidChange()
    if rounded {
    backgroundColor = tintColor
    }
    }

    // swiftlint:enable override_in_extension
    }

    extension UIBarButtonItem {
    public extension UIBarButtonItem {
    convenience init(badge: String?, title: String, target: AnyObject?, action: Selector) {
    let button = UIButton(type: .System)
    button.setTitle(title, forState: .Normal)
    button.titleLabel?.font = UIFont.systemFontOfSize(UIFont.buttonFontSize())
    button.addTarget(target, action: action, forControlEvents: .TouchUpInside)
    let button = UIButton(type: .system)
    button.setTitle(title, for: .normal)
    button.titleLabel?.font = UIFont.systemFont(ofSize: UIFont.buttonFontSize)
    button.addTarget(target, action: action, for: .touchUpInside)
    button.sizeToFit()

    let badgeLabel = UILabel(badgeText: badge ?? "")
    button.addSubview(badgeLabel)
    button.addConstraint(NSLayoutConstraint(item: badgeLabel, attribute: .Top, relatedBy: .Equal, toItem: button, attribute: .Top, multiplier: 1, constant: 0))
    button.addConstraint(NSLayoutConstraint(item: badgeLabel, attribute: .CenterX, relatedBy: .Equal, toItem: button, attribute: .Trailing, multiplier: 1, constant: 0))
    if nil == badge {
    badgeLabel.hidden = true
    if let badge = badge {
    let badgeLabel = UILabel(badgeText: badge)
    button.addSubview(badgeLabel)
    button.addConstraint(NSLayoutConstraint(item: badgeLabel, attribute: .top, relatedBy: .equal, toItem: button, attribute: .top, multiplier: 1, constant: 0))
    button.addConstraint(NSLayoutConstraint(item: badgeLabel, attribute: .centerX, relatedBy: .equal, toItem: button, attribute: .trailing, multiplier: 1, constant: 0))
    }
    badgeLabel.tag = UIBarButtonItem.badgeTag

    self.init(customView: button)
    }
    @@ -116,19 +124,19 @@ extension UIBarButtonItem {
    }

    var badgeString: String? {
    get { return badgeLabel?.text?.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()) }
    get { return badgeLabel?.text?.trimmingCharacters(in: .whitespaces) }
    set {
    if let badgeLabel = badgeLabel {
    badgeLabel.text = nil == newValue ? nil : " \(newValue!) "
    badgeLabel.sizeToFit()
    badgeLabel.hidden = nil == newValue
    badgeLabel.isHidden = nil == newValue
    }
    }
    }

    var badgedTitle: String? {
    get { return badgedButton?.titleForState(.Normal) }
    set { badgedButton?.setTitle(newValue, forState: .Normal); badgedButton?.sizeToFit() }
    get { return badgedButton?.title(for: .normal) }
    set { badgedButton?.setTitle(newValue, for: .normal); badgedButton?.sizeToFit() }
    }

    private static let badgeTag = 7373
  4. yonat revised this gist Feb 16, 2016. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions Badge.swift
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,11 @@
    // Badge.swift
    // Extensions for Rounded UILabel and UIButton, Badged UIBarButtonItem.
    //
    // Usage:
    // let label = UILabel(badgeText: "Rounded Label");
    // let button = UIButton(type: .System); button.rounded = true
    // let barButton = UIBarButtonItem(badge: "42", title: "How Many Roads", target: self, action: "answer")
    //
    // Created by Yonat Sharon on 06.04.2015.
    // Copyright (c) 2015 Yonat Sharon. All rights reserved.
    //
  5. yonat revised this gist Oct 27, 2015. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions Badge.swift
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@ extension UILabel {
    layer.cornerRadius = fontSize * CGFloat(0.6)
    clipsToBounds = true

    setTranslatesAutoresizingMaskIntoConstraints(false)
    translatesAutoresizingMaskIntoConstraints = false
    addConstraint(NSLayoutConstraint(item: self, attribute: .Width, relatedBy: .GreaterThanOrEqual, toItem: self, attribute: .Height, multiplier: 1, constant: 0))
    }
    }
    @@ -43,7 +43,9 @@ extension UIButton {
    setTitle(currentTitle, forState: .Normal)
    }
    else {
    let attrTitle = NSAttributedString(string: currentTitle ?? "", attributes: [NSFontAttributeName: UIFont.systemFontOfSize(newValue)])
    let attrTitle = NSAttributedString(string: currentTitle ?? "", attributes:
    [NSFontAttributeName: UIFont.systemFontOfSize(newValue), NSForegroundColorAttributeName: currentTitleColor]
    )
    setAttributedTitle(attrTitle, forState: .Normal)
    }

    @@ -82,7 +84,7 @@ extension UIButton {

    extension UIBarButtonItem {
    convenience init(badge: String?, title: String, target: AnyObject?, action: Selector) {
    let button = UIButton.buttonWithType(.System) as! UIButton
    let button = UIButton(type: .System)
    button.setTitle(title, forState: .Normal)
    button.titleLabel?.font = UIFont.systemFontOfSize(UIFont.buttonFontSize())
    button.addTarget(target, action: action, forControlEvents: .TouchUpInside)
  6. yonat revised this gist Apr 13, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Badge.swift
    Original file line number Diff line number Diff line change
    @@ -123,6 +123,6 @@ extension UIBarButtonItem {
    get { return badgedButton?.titleForState(.Normal) }
    set { badgedButton?.setTitle(newValue, forState: .Normal); badgedButton?.sizeToFit() }
    }

    private static let badgeTag = 7373
    }
  7. yonat revised this gist Apr 13, 2015. No changes.
  8. yonat revised this gist Apr 13, 2015. 1 changed file with 43 additions and 5 deletions.
    48 changes: 43 additions & 5 deletions Badge.swift
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,13 @@
    //
    // Badge.swift
    // Extensions for Rounded UILabel and UIButton, Badged UIBarButtonItem.
    //
    // Created by Yonat Sharon on 06.04.2015.
    // Copyright (c) 2015 Yonat Sharon. All rights reserved.
    //

    import UIKit

    extension UILabel {
    convenience init(badgeText: String, color: UIColor = UIColor.redColor(), fontSize: CGFloat = UIFont.smallSystemFontSize()) {
    self.init()
    @@ -78,13 +88,41 @@ extension UIBarButtonItem {
    button.addTarget(target, action: action, forControlEvents: .TouchUpInside)
    button.sizeToFit()

    if let badge = badge {
    let badgeLabel = UILabel(badgeText: badge)
    button.addSubview(badgeLabel)
    button.addConstraint(NSLayoutConstraint(item: badgeLabel, attribute: .Top, relatedBy: .Equal, toItem: button, attribute: .Top, multiplier: 1, constant: 0))
    button.addConstraint(NSLayoutConstraint(item: badgeLabel, attribute: .CenterX, relatedBy: .Equal, toItem: button, attribute: .Trailing, multiplier: 1, constant: 0))
    let badgeLabel = UILabel(badgeText: badge ?? "")
    button.addSubview(badgeLabel)
    button.addConstraint(NSLayoutConstraint(item: badgeLabel, attribute: .Top, relatedBy: .Equal, toItem: button, attribute: .Top, multiplier: 1, constant: 0))
    button.addConstraint(NSLayoutConstraint(item: badgeLabel, attribute: .CenterX, relatedBy: .Equal, toItem: button, attribute: .Trailing, multiplier: 1, constant: 0))
    if nil == badge {
    badgeLabel.hidden = true
    }
    badgeLabel.tag = UIBarButtonItem.badgeTag

    self.init(customView: button)
    }

    var badgeLabel: UILabel? {
    return customView?.viewWithTag(UIBarButtonItem.badgeTag) as? UILabel
    }

    var badgedButton: UIButton? {
    return customView as? UIButton
    }

    var badgeString: String? {
    get { return badgeLabel?.text?.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()) }
    set {
    if let badgeLabel = badgeLabel {
    badgeLabel.text = nil == newValue ? nil : " \(newValue!) "
    badgeLabel.sizeToFit()
    badgeLabel.hidden = nil == newValue
    }
    }
    }

    var badgedTitle: String? {
    get { return badgedButton?.titleForState(.Normal) }
    set { badgedButton?.setTitle(newValue, forState: .Normal); badgedButton?.sizeToFit() }
    }

    private static let badgeTag = 7373
    }
  9. yonat revised this gist Apr 10, 2015. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions Badge.swift
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,13 @@
    //
    // Badge.swift
    // Extensions for Rounded UILabel and UIButton, Badged UIBarButtonItem.
    //
    // Created by Yonat Sharon on 06.04.2015.
    // Copyright (c) 2015 Yonat Sharon. All rights reserved.
    //

    import UIKit

    extension UILabel {
    convenience init(badgeText: String, color: UIColor = UIColor.redColor(), fontSize: CGFloat = UIFont.smallSystemFontSize()) {
    self.init()
  10. yonat revised this gist Apr 7, 2015. 1 changed file with 24 additions and 5 deletions.
    29 changes: 24 additions & 5 deletions Badge.swift
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,7 @@ extension UIButton {
    get { return layer.cornerRadius > 0 }
    set { roundWithTitleSize(newValue ? titleSize : 0) }
    }

    /// removes other title attributes
    var titleSize: CGFloat {
    get {
    @@ -36,18 +36,37 @@ extension UIButton {
    let attrTitle = NSAttributedString(string: currentTitle ?? "", attributes: [NSFontAttributeName: UIFont.systemFontOfSize(newValue)])
    setAttributedTitle(attrTitle, forState: .Normal)
    }

    if rounded {
    roundWithTitleSize(newValue)
    }
    }
    }
    private func roundWithTitleSize(size: CGFloat) {

    func roundWithTitleSize(size: CGFloat) {
    let padding = size / 4
    layer.cornerRadius = padding + size*1.2/2
    layer.cornerRadius = padding + size * 1.2 / 2
    let sidePadding = padding * 1.5
    contentEdgeInsets = UIEdgeInsets(top: padding, left: sidePadding, bottom: padding, right: sidePadding)

    if size.isZero {
    backgroundColor = UIColor.clearColor()
    setTitleColor(tintColor, forState: .Normal)
    }
    else {
    backgroundColor = tintColor
    let currentTitleColor = titleColorForState(.Normal)
    if currentTitleColor == nil || currentTitleColor == tintColor {
    setTitleColor(UIColor.whiteColor(), forState: .Normal)
    }
    }
    }

    override public func tintColorDidChange() {
    super.tintColorDidChange()
    if rounded {
    backgroundColor = tintColor
    }
    }
    }

  11. yonat revised this gist Mar 28, 2015. 1 changed file with 19 additions and 0 deletions.
    19 changes: 19 additions & 0 deletions Badge.swift
    Original file line number Diff line number Diff line change
    @@ -50,3 +50,22 @@ extension UIButton {
    contentEdgeInsets = UIEdgeInsets(top: padding, left: sidePadding, bottom: padding, right: sidePadding)
    }
    }

    extension UIBarButtonItem {
    convenience init(badge: String?, title: String, target: AnyObject?, action: Selector) {
    let button = UIButton.buttonWithType(.System) as! UIButton
    button.setTitle(title, forState: .Normal)
    button.titleLabel?.font = UIFont.systemFontOfSize(UIFont.buttonFontSize())
    button.addTarget(target, action: action, forControlEvents: .TouchUpInside)
    button.sizeToFit()

    if let badge = badge {
    let badgeLabel = UILabel(badgeText: badge)
    button.addSubview(badgeLabel)
    button.addConstraint(NSLayoutConstraint(item: badgeLabel, attribute: .Top, relatedBy: .Equal, toItem: button, attribute: .Top, multiplier: 1, constant: 0))
    button.addConstraint(NSLayoutConstraint(item: badgeLabel, attribute: .CenterX, relatedBy: .Equal, toItem: button, attribute: .Trailing, multiplier: 1, constant: 0))
    }

    self.init(customView: button)
    }
    }
  12. yonat created this gist Mar 28, 2015.
    52 changes: 52 additions & 0 deletions Badge.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    extension UILabel {
    convenience init(badgeText: String, color: UIColor = UIColor.redColor(), fontSize: CGFloat = UIFont.smallSystemFontSize()) {
    self.init()
    text = " \(badgeText) "
    textColor = UIColor.whiteColor()
    backgroundColor = color

    font = UIFont.systemFontOfSize(fontSize)
    layer.cornerRadius = fontSize * CGFloat(0.6)
    clipsToBounds = true

    setTranslatesAutoresizingMaskIntoConstraints(false)
    addConstraint(NSLayoutConstraint(item: self, attribute: .Width, relatedBy: .GreaterThanOrEqual, toItem: self, attribute: .Height, multiplier: 1, constant: 0))
    }
    }

    extension UIButton {
    /// show background as rounded rect, like mail addressees
    var rounded: Bool {
    get { return layer.cornerRadius > 0 }
    set { roundWithTitleSize(newValue ? titleSize : 0) }
    }

    /// removes other title attributes
    var titleSize: CGFloat {
    get {
    let titleFont = attributedTitleForState(.Normal)?.attribute(NSFontAttributeName, atIndex: 0, effectiveRange: nil) as? UIFont
    return titleFont?.pointSize ?? UIFont.buttonFontSize()
    }
    set {
    // TODO: use current attributedTitleForState(.Normal) if defined
    if UIFont.buttonFontSize() == newValue || 0 == newValue {
    setTitle(currentTitle, forState: .Normal)
    }
    else {
    let attrTitle = NSAttributedString(string: currentTitle ?? "", attributes: [NSFontAttributeName: UIFont.systemFontOfSize(newValue)])
    setAttributedTitle(attrTitle, forState: .Normal)
    }

    if rounded {
    roundWithTitleSize(newValue)
    }
    }
    }

    private func roundWithTitleSize(size: CGFloat) {
    let padding = size / 4
    layer.cornerRadius = padding + size*1.2/2
    let sidePadding = padding * 1.5
    contentEdgeInsets = UIEdgeInsets(top: padding, left: sidePadding, bottom: padding, right: sidePadding)
    }
    }