Introduction
IncrementableLabel is a lightweight and customizable UILabel subclass that adds increment and decrement functionality. It allows users to easily increase or decrease the label’s value with just a single tap or long press gesture.
Features
- Simple and intuitive increment and decrement functionality
- Customizable appearance and behavior
- Supports both tap and long press gestures
- Automatic value wrapping when reaching maximum or minimum limits
- Option to set minimum and maximum bounds
Usage
Installation
To install IncrementableLabel, you can use CocoaPods. Simply add the following line to your Podfile:
pod 'IncrementableLabel'
Importing
To use IncrementableLabel in your project, import it in your Swift file:
import IncrementableLabel
Creating an Instance
// Instantiate an IncrementableLabel with initial value
let incrementableLabel = IncrementableLabel(value: 0)
// Add the label to your view hierarchy
view.addSubview(incrementableLabel)
Customizing Appearance
IncrementableLabel provides various properties to customize its appearance:
// Set font
incrementableLabel.font = UIFont.systemFont(ofSize: 18)
// Set text color
incrementableLabel.textColor = UIColor.black
// Set background color
incrementableLabel.backgroundColor = UIColor.white
// Set corner radius
incrementableLabel.layer.cornerRadius = 8
// Set border color and width
incrementableLabel.layer.borderColor = UIColor.gray.cgColor
incrementableLabel.layer.borderWidth = 1
// Set padding
incrementableLabel.padding = UIEdgeInsets(top: 4, left: 8, bottom: 4, right: 8)
Customizing Behavior
You can further customize the behavior of the IncrementableLabel by setting the following properties:
// Set increment and decrement step values
incrementableLabel.incrementStep = 1
incrementableLabel.decrementStep = 1
// Set gesture type (default: .tap)
incrementableLabel.gestureType = .longPress
// Set gesture duration (default: 0.2)
incrementableLabel.gestureDuration = 0.5
// Set maximum and minimum bounds
incrementableLabel.maximumValue = 100
incrementableLabel.minimumValue = -50
Conclusion
With IncrementableLabel, you can easily implement increment and decrement functionality in your UILabels. Its flexibility and customizability make it a great addition to your iOS projects.
Reference
For more information and detailed usage examples, please refer to the official IncrementableLabel repository on GitHub.
Change Log
- 1.0.0 – Initial release
- 1.0.1 – Bug fixes and performance improvements
- 1.1.0 – Added support for long press gestures