Generate a badge for any UIView In Swift
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 lightweight, easy to understand approach to make a circular UILabel with the given text
- platform: iOS
- extends: UIView
- @discardableResult public func setBadge(in direction: BadgeDirection, with text: String) -> BadgeLabel
- adds a new badge to the view in the given location (BadgeDirection)
- adds a new badge to the view in the given location (BadgeDirection)
- @discardableResult public func setBadge(in direction: BadgeDirection, with text: String) -> BadgeLabel
Installation:
In your Xcode project, simply go to File -> Swift Packages -> Add Package Dependency.
Then use this repo's URL: https://github.com/froggomad/BadgeGenerator
Usage:
Create, position, and hold reference to a badge
let myView = UIView()
let badge = myView.setBadge(in: .northWest, with: "1")
Increment an integer value by n
This increments the value if it can be converted to Int and update's the badge's text
badge.incrementIntValue(by: 1)
Attempt to increment an integer value, and get the result back
It will fail if the text
value can't be converted to Int
let value = badge.incrementIntValue(by: 1)
switch value {
case let .success(intValue):
print(intValue)
case let .failure(error):
print(error)
}
Change a badge's text to "foo"
badge.set("foo")
Remove a badge
badge.remove()
Example Badges
These badges are placed on a PreviewProvider
, but you can place them on any UIView or in SwiftUI using UIViewRepresentable