/swift-badge

Badge view for iOS written in swift with Extensions

Primary LanguageSwiftMIT LicenseMIT

Swift Badge

Badge control for iOS written in Swift

  • The badge is a subclass of UILabel view.
  • It can be created and customized from the Storyboard or programatically.
  • Supports iOS 7+.

Usage

Copy SwiftBadge.swift to your project.

Creating a badge in Storyboard

  • Drag a Label to your view.
  • Set its class to SwiftBadge in identity inspector.

Add badge to storyboard

  • Set the badge properties in the attributes inspector.

Change badge properties in attribute inspector

Creating a badge from code

let badge = SwiftBadge()
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.preferredFontForTextStyle(UIFontTextStyleBody)

// Text color
badge.textColor = UIColor.yellowColor()

// Badge color
badge.badgeColor = UIColor.blackColor()

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

// No shadow
badge.shadowOpacityBadge = 0

// Border width and color
badge.borderWidth = 5.0
badge.borderColor = UIColor.magentaColor()

Demo app

This project includes a demo app.

Swift badge for iOS demo app

Show Swift badge in a table view in iOS

Create badge from code Swift badge for iOS

Alternative solutions

Here are some alternative badges for iOS.

Contributors

  • Thanks to amg1976 for adding a border and redesigning the drawing.

License

SwiftBadge 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.