A badge view for iOS/tvOS written in Swift

Related tags

Badges swift ios badge
Overview

Swift Badge

A badge control for iOS and tvOS written in Swift

Carthage compatible CocoaPods Version License Platform

  • The badge is a subclass of UILabel view.
  • It can be created and customized from the Storyboard or from the code.

Setup

There are various ways you can add BadgeSwift to your Xcode project.

Add source (iOS 7+)

Simply add BadgeSwift.swift file to your project.

Setup with Carthage (iOS 8+)

Alternatively, add github "evgenyneu/swift-badge" ~> 8.0 to your Cartfile and run carthage update.

Setup with CocoaPods (iOS 8+)

If you are using CocoaPods add this text to your Podfile and run pod install.

use_frameworks!
target 'Your target name'
pod 'BadgeSwift', '~> 8.0'

Setup with Swift Package Manager

Legacy Swift versions

Setup a previous version of the library if you use an older version of Swift.

Usage

Creating a badge in the Storyboard

  • Drag a Label to your view.
  • Set its class to BadgeSwift in identity inspector.
  • Set the module property to BadgeSwift (unless you used the file setup method).

Add badge to storyboard

  • Customize the badge properties in the attributes inspector (text, color and other).
  • If storyboard does not show the badge correctly click Refresh All Views from the Editor menu.

Change badge properties in attribute inspector

Note: Carthage setup method does not allow to customize Cosmos view from the storyboard, please do it from code instead.

Creating a badge from the code

Add import BadgeSwift to your source code unless you used the file setup method.

let badge = BadgeSwift()
view.addSubview(badge)
// Position the badge ...

See example of how to create and position the badge from code in the demo app.

Customization

// Text
badge.text = "2"

// Insets
badge.insets = CGSize(width: 12, height: 12)

// Font
badge.font = UIFont.preferredFont(forTextStyle: UIFontTextStyle.body)

// Text color
badge.textColor = UIColor.yellow

// Badge color
badge.badgeColor = UIColor.black

// Shadow
badge.shadowOpacityBadge = 0.5
badge.shadowOffsetBadge = CGSize(width: 0, height: 0)
badge.shadowRadiusBadge = 1.0
badge.shadowColorBadge = UIColor.black

// No shadow
badge.shadowOpacityBadge = 0

// Border width and color
badge.borderWidth = 5.0
badge.borderColor = UIColor.magenta

// Customize the badge corner radius.
// -1 if unspecified. When unspecified, the corner is fully rounded. Default: -1.
badge.cornerRadius = 10

Demo app

This project includes a demo app.

BadgeSwift for iOS demo app

Show BadgeSwift in a table view in iOS

Create badge from code BadgeSwift for iOS

Alternative solutions

Here are some alternative badges for iOS.

Thanks to 👍

  • amg1976 for adding a border and redesigning the drawing.
  • gperdomor for adding ability to customize corner radius.
  • acecilia for removing a warning when the badge is used in an extension.

License

BadgeSwift is released under the MIT License.

Feedback is welcome

If you found a bug or want to improve the badge feel free to create an issue.

Comments
  • .Hidden

    .Hidden

    The .hidden property on the badge seems to hide only the red background. The white text still remains visible. I have tried setting it to "" but it remains the previous value. Any ideas? Thanks

    if listingsToLoad[indexPath.row].unReadMessages.value != nil {
        cell.cellBadge.hidden = false
        cell.cellBadge.text = String(listingsToLoad[indexPath.row].unReadMessages.value!)
    } else {
        cell.cellBadge.hidden = true
        cell.cellBadge.text = ""
    }
    
    opened by uknowmeright 6
  • ld: warning: linking against a dylib which is not safe for use in application extensions

    ld: warning: linking against a dylib which is not safe for use in application extensions

    As explained here, there are some APIs unavailable for extensions. Seems like BadgeSwift is using them, because I am getting the error:

    ld: warning: linking against a dylib which is not safe for use in application extensions

    Any idea which is the part of the code that is using this kind of APIs? Looking at the functionality of this framework, it should be possible to use it in extensions.

    opened by acecilia 5
  • Changed how the background was drawn

    Changed how the background was drawn

    Thanks for distributing this project, I was trying to achieve the same result but was going on a much more complicated solution!

    In order to correctly support a border on the badge, I needed to refactor how the background was being drawn, because the end result wasn't correct, in my opinion. screen shot 2015-11-28 at 11 42 11 a m So instead of setting the background color on the layer to the desired color, the layer is now always clearColor and a UIBezierPath is used instead, with fill color based on the UILabel background color. This way, if the value of borderWidth is set to a value bigger than 0, it will set the stroke color and line width of the UIBezierPath.

    opened by amg1976 5
  • Various compile errors with example

    Various compile errors with example

    swift-badge-master/swift-badge/SwiftBadge.swift:15:12: Initializer does not override a designated initializer from its superclass

    swift-badge-master/swift-badge/SwiftBadge.swift:16:5: Must call a designated initializer of the superclass 'UILabel'

    swift-badge-master/swift-badge/ViewController.swift:50:12: 'NSString' is not implicitly convertible to 'String'; did you mean to use 'as' to explicitly convert?

    I' m using latest Xcode and swift.

    opened by xgdgsc 5
  • The small red part

    The small red part

    Please consider submitting the following information (if relevant):

    • Library setup method: CocoaPods.
    • Version of the library. Example: 5.0.1.
    • Xcode version. Example: 9.4.1.
    • OS version. Example: iOS 11.4.

    The small red part that appears to be cut off on the top right always pops up. What should I do in order to remove it?

    img_2880

    Thank you!

    opened by elibooklover 3
  • Swift 4.2

    Swift 4.2

    Hi,

    I'm using the last version of the library from cocoapods (BadgeSwift (5.0.1)) and it doesn't build on Swift4.2. Is there any plan to update it? It's just one line of code actually: let rectWithoutInsets = UIEdgeInsetsInsetRect(rect, insets) to let rectWithoutInsets = rect.inset(by: insets)

    Thanks,

    opened by wolfAle 2
  • Subclassing when using CocoaPods

    Subclassing when using CocoaPods

    Hi!

    I wanted to subclass BadgeSwift to add some custom default styling. But when i tried i got..

    Cannot inherit from non-open class 'BadgeSwift' outside of it´s defining module.

    I´m using CocoaPods. I guess this is just because of the missing open declaration. Could we add this? Or is the class intentionally non-open?

    opened by r-dent 2
  • What version is in Swift 2.3?

    What version is in Swift 2.3?

    I am having trouble finding what Version supports swift 2.3?

    I have gone as fat back as 2.011. Could you include this in the docs for future reference?

    Thanks!

    opened by uknowmeright 2
  • Add corner radius support

    Add corner radius support

    Quick fixes to support corner radius.

    For some reason if the corner radius is 0, and the label has border, the top left edge is cut, but i don't know why. Please check that when you have time :D

    opened by gperdomor 1
  • Render Issue on Storyboad

    Render Issue on Storyboad

    I have added Bade on story and then, it start complaining some random error.

    Failed to render and update auto layout status for [My View Controller Name].swift "
    Required code signature missing for BadeSwift.framework missing. 
    
    • Library setup method: file, Carthage, CocoaPods or Swift Package Manager.
    • Version of the library : 5.0.1
    • Xcode version. Example: 9.3
    • OS version. Example: iOS 11
    opened by greenSyntax 1
  • When the badge becomes small ( eg 20x20 ) , it doesnt properly display double digit numbers.

    When the badge becomes small ( eg 20x20 ) , it doesnt properly display double digit numbers.

    Please consider submitting the following information (if relevant):

    • Library setup method: file, Carthage, CocoaPods or Swift Package Manager.
    • Version of the library. Example: 8.0.
    • Xcode version. Example: 8.3.3.
    • OS version. Example: iOS 10.3.2.
    opened by Return-1 2
Owner
Evgenii Neumerzhitckii
🐋🦔🐢🐝wow
Evgenii Neumerzhitckii
iOS custom badge button.

MIBadgeButton MIBadgeButton is badge button written in Swift with high UITableView/UICollectionView performance. Screenshots Installation MIBadgeButto

Mustafa Ibrahim 344 Dec 28, 2022
Generate a badge for any UIView

Badge Generator Generate a "badge" (circular label) for any view and place it in the center or any corner of a given view Badge Generator uses a light

Kenny Dubroff (froggomad) 11 Jan 6, 2022
UIView extension that adds a notification badge.

Installation CocoaPods pod 'EasyNotificationBadge' Swift Package Manager You can use The Swift Package Manager to install EasyNotificationBadge by add

Antonio Zaitoun 215 Dec 11, 2022
A way to quickly add a notification badge icon to any view. Make any view of a full-fledged animated notification center.

BadgeHub A way to quickly add a notification badge icon to any view. Demo/Example For demo: $ pod try BadgeHub To run the example project, clone the r

Jogendra 772 Dec 28, 2022
A way to quickly add a notification badge icon to any view. Make any view of a full-fledged animated notification center.

BadgeHub A way to quickly add a notification badge icon to any view. Demo/Example For demo: $ pod try BadgeHub To run the example project, clone the r

Jogendra 773 Dec 30, 2022
iOS custom badge button.

MIBadgeButton MIBadgeButton is badge button written in Swift with high UITableView/UICollectionView performance. Screenshots Installation MIBadgeButto

Mustafa Ibrahim 344 Dec 28, 2022
Generate a badge for any UIView

Badge Generator Generate a "badge" (circular label) for any view and place it in the center or any corner of a given view Badge Generator uses a light

Kenny Dubroff (froggomad) 11 Jan 6, 2022
UIView extension that adds a notification badge.

Installation CocoaPods pod 'EasyNotificationBadge' Swift Package Manager You can use The Swift Package Manager to install EasyNotificationBadge by add

Antonio Zaitoun 215 Dec 11, 2022
An image download extension of the image view written in Swift for iOS, tvOS and macOS.

Moa, an image downloader written in Swift for iOS, tvOS and macOS Moa is an image download library written in Swift. It allows to download and show an

Evgenii Neumerzhitckii 330 Sep 9, 2022
Cachyr A typesafe key-value data cache for iOS, macOS, tvOS and watchOS written in Swift.

Cachyr A typesafe key-value data cache for iOS, macOS, tvOS and watchOS written in Swift. There already exists plenty of cache solutions, so why creat

Norsk rikskringkasting (NRK) 124 Nov 24, 2022
SwiftMoment - A time and calendar manipulation library for iOS 9+, macOS 10.11+, tvOS 9+, watchOS 2+ written in Swift 4.

SwiftMoment This framework is inspired by Moment.js. Its objectives are the following: Simplify the manipulation and readability of date and interval

Adrian Kosmaczewski 1.6k Dec 31, 2022
BrickKit is a delightful layout library for iOS and tvOS. It is written entirely in Swift!

BrickKit is a delightful layout library for iOS and tvOS. It is written entirely in Swift! Deprecated BrickKit is being phased out at Wayfair, and the

Wayfair Tech – Archive 608 Sep 15, 2022
An Impressive Auto Layout DSL for iOS, tvOS & OSX. & It is written in pure swift.

KVConstraintKit KVConstraintKit is a DSL to make easy & impressive Auto Layout constraints on iOS, tvOS & OSX with Swift Installation Using CocoaPods

Keshav Vishwkarma 90 Sep 1, 2022
An NSPredicate DSL for iOS, OSX, tvOS, & watchOS. Inspired by SnapKit and lovingly written in Swift.

PrediKit A Swift NSPredicate DSL for iOS & OS X inspired by SnapKit, lovingly written in Swift, and created by that weird dude at KrakenDev. If you're

Hector Matos 542 Sep 24, 2022
EventBroadcaster is a lightweight event handler framework, written in swift for iOS, macOS, tvOS & watchOS applications.

EventBroadcaster is a lightweight event handler framework, written in swift for iOS, macOS, tvOS & watchOS applications.

Ali Samaiee 4 Oct 5, 2022
Matomo iOS, tvOS and macOS SDK: a Matomo tracker written in Swift

MatomoTracker (former PiwikTracker) iOS SDK The MatomoTracker is an iOS, tvOS and macOS SDK for sending app analytics to a Matomo server. MatomoTracke

Matomo Analytics 367 Dec 17, 2022
An NSPredicate DSL for iOS, OSX, tvOS, & watchOS. Inspired by SnapKit and lovingly written in Swift.

PrediKit A Swift NSPredicate DSL for iOS & OS X inspired by SnapKit, lovingly written in Swift, and created by that weird dude at KrakenDev. If you're

Hector Matos 542 Sep 24, 2022
A star rating control for iOS/tvOS written in Swift

Cosmos, a star rating control for iOS and tvOS This is a UI control for iOS and tvOS written in Swift. It shows a star rating and takes rating input f

Evgenii Neumerzhitckii 2.1k Dec 28, 2022
Timekeeper is an easy-to-use time measurement library written in Swift, with support for iOS, tvOS, watchOS and macOS.

Timekeeper is an easy-to-use time measurement library written in Swift, with support for iOS, tvOS, watchOS and macOS. Installation Timekee

Andreas Pfurtscheller 1 Oct 18, 2021
A sound fader for AVAudioPlayer written in Swift for iOS, tvOS and macOS.

Cephalopod, a sound fader for AvAudioPlayer written in Swift - iOS, tvOS and macOS This library can help fading sounds in and out with AvAudioPlayer.

Evgenii Neumerzhitckii 109 Dec 16, 2022