This is a badge view for iOS 7+ written in Swift. The style is similar to the springboard badges in iOS 7 or 8. The view is a subclass of UILabel view.
Copy SwiftBadge.swift to your project. A badge can be created and positioned similar to any UIView.
Swift badge is written in Swift 2.0 for Xcode 7. To use it with Swift 1.2 get the SwiftBadge.swift for Xcode 6 or checkout the swift-1.2
tag.
- Drag a Label to your view.
- Set its
class
toSwiftBadge
in identity inspector.
- Add auto layout constraints to position the badge.
- Font can be set in the attribute inspector.
- Colors and shadow can only be customized in code (see below).
Note:
- Badge will still look like an ordinary label in storyboard.
- There is no need to set size or width/height constraints for the badge in the storyboard. The size will be determined dynamically based on badge text. You can see how it is done in this demo app.
let badge = SwiftBadge()
view.addSubview(badge)
// text
badge.text = '2'
// insets
badge.defaultInsets = CGSize(width: 12, height: 12)
// font
badge.font = UIFont.preferredFontForTextStyle(UIFontTextStyleBody)
// text color
badge.textColor = UIColor.yellowColor()
// background color
badge.layer.backgroundColor = UIColor.blackColor().CGColor
// shadow
badge.layer.shadowOpacity = 0.5
badge.layer.shadowOffset = CGSize(width: 0, height: 0)
badge.layer.shadowRadius = 1.0
badge.layer.shadowColor = UIColor.blackColor().CGColor
// no shadow
badge.layer.shadowOpacity = 0
// set border width and color
badge.borderWidth = 2.0
badge.borderColor = UIColor.yellowColor()
Tip: try calling badge.invalidateIntrinsicContentSize()
if propery changes are not updated.
There is a great alternative badge created by Sascha Paulus in Objective-C.