jansvancer / BadgeControl

Badge view with controller written in Swift 4

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

defaultboomall

BadgeControl

  • Simply attach BadgeController to any UIView's subclasses and easily control the badge.
  • Customize size, position, background color, text color and animation of the badge.

Setup

Setup with CocoaPods (iOS 9+)

  • If you are using CocoaPods add this text to your Podfile and run pod install.
    pod 'BadgeControl', :git => 'https://github.com/kiwisip/BadgeControl.git'

Add source (iOS 9+)

Usage

Add import BadgeControl to your source code (unless you used the file setup method).

Simply attach BadgeController to your UIView (or its subclass).

let badge = BadgeController(for: myUIView)

Add a text to your badge and present it with animation.

badge.addOrReplaceCurrent(with text: "1", animated: true)

Remove badge from its view.

badge.remove()

Simply increment or decrement the value on your badge (if it is numeric) and present it with animation.

badge.increment(animated: true)
badge.decrement(animated: true)

Customization

You can customize badge's text font, background color, text color, size, center position and animation.

Text font, background color, text color and size

badge.badgeTextFont = UIFont.systemFont(ofSize: 15)
badge.badgeBackgroundColor = UIColor.blue
badge.badgeTextColor = UIColor.yellow
badge.badgeSizeResizingRatio = 2 // badge will be 2x bigger than default

Center position

You can choose following center positions: upperLeftCorner, upperRightCorner, lowerLeftCorner, lowerRightCorner.

You can also choose custom position by calling .custom(x: Double, y: Double).

badge.centerPosition = .upperLeftCorner
badge.centerPosition = .custom(x: 10, y: 20)

Animations

You can choose one of the following animations:

Default animation

default

badge.animation = BadgeAnimations.default
Left-right animation

default

badge.animation = BadgeAnimations.leftRight
Right-left animation

default

badge.animation = BadgeAnimations.rightLeft
Fade in animation

default

badge.animation = BadgeAnimations.fadeIn
Rolling animation

default

badge.animation = BadgeAnimations.rolling
Custom

You can also provide your own animation of type ((UIView) -> Void).

badge.animation = { badgeView in
    ...
  }

Initialization

You can use one of these initalizers for your convenience:

init(for view: UIView, badgeSizeResizingRatio: CGFloat = 1)
init(for view: UIView, in centerPosition: CenterPosition, badgeSizeResizingRatio: CGFloat = 1)
init(for view: UIView, badgeBackgroundColor: UIColor, badgeTextColor: UIColor, badgeSizeResizingRatio: CGFloat = 1)
init(for view: UIView, in centerPosition: CenterPosition, badgeBackgroundColor: UIColor, badgeTextColor: UIColor, badgeSizeResizingRatio: CGFloat = 1)
init(for view: UIView, badgeSizeResizingRatio: CGFloat = 1, animation: ((UIView) -> Void)?)
init(for view: UIView, in centerPosition: CenterPosition, badgeSizeResizingRatio: CG Float = 1, animation: ((UIView) -> Void)?)
init(for view: UIView,badgeBackgroundColor: UIColor, badgeTextColor: UIColor, badgeSizeResizingRatio: CGFloat = 1, animation: ((UIView) -> Void)?)
init(for view: UIView, in centerPosition: CenterPosition, badgeBackgroundColor: UIColor, badgeTextColor: UIColor, badgeSizeResizingRatio: CGFloat = 1, animation: ((UIView) -> Void)?)

License

BadgeControl is released under the MIT License.

Feedback is welcome

If you've found a bug or want to improve BadgeControl feel free to create an issue.

About

Badge view with controller written in Swift 4

License:MIT License


Languages

Language:Swift 73.8%Language:Shell 18.9%Language:Objective-C 3.7%Language:Ruby 3.5%