Home

Awesome

<img src='graphics/swift_badge_showcase_2.png' width='419' alt='Swift Badge'>

A badge control for iOS and tvOS written in Swift

Carthage compatible CocoaPods Version License Platform

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

<img src='https://raw.githubusercontent.com/evgenyneu/swift-badge/master/graphics/swift_badge_class_name_3.png' width='258' alt='Add badge to storyboard'> <img src='https://github.com/evgenyneu/swift-badge/blob/master/graphics/swift_badge_attributes_inspector_2.png' width='374' alt='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.

<img src='graphics/demo_app/swift_badge_ios_demo_app.png' width='320' alt='BadgeSwift for iOS demo app'> <img src='graphics/demo_app/swift_badge_ios_in_table_view.png' width='320' alt='Show BadgeSwift in a table view in iOS'> <img src='graphics/demo_app/swift_badge_create_from_code.png' width='320' alt='Create badge from code BadgeSwift for iOS'>

Alternative solutions

Here are some alternative badges for iOS.

Thanks to 👍

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.