RotatingLabel is a label that animates text changes by scrolling characters. The component is designed for presenting numeric values, such as account balances and stock prices, but it can be used for any text, although not ideal.
To install RotatingLabel using Swift Package Manager, add the following to your Package.swift
file:
.package(url: "https://github.com/raymondjavaxx/RotatingLabel.git", from: "1.0.0")
Or, follow the instructions on Adding package dependencies to your app guide.
Usage is simple. Create a RotatingLabel and set the text property to the value you want to display. When it is time to change the value, use the setText(_:animated:)
method to animate it.
let label = RotatingLabel()
label.text = "$100.00"
// ...
label.setText("$155.00", animated: true)
You can customize the animation length and timing parameters by setting the animationDuration
and animationTimingParameters
properties.
label.animationDuration = 0.3
label.animationTimingParameters = UICubicTimingParameters(animationCurve: .easeInOut)
RotatingLabel uses a diffing function to determine which characters need to be animated. You can use the diffingFunction
property to use any of the built-in diffing functions or provide your own.
label.diffingFunction = DiffingFunction { oldValue, newValue in
var changes: [Operation] = []
// Your custom diffing logic here
return changes
}
RotatingLabel supports Dynamic Type. The label can automatically adjust its font size when the user changes the content size category. To enable it, set the adjustsFontForContentSizeCategory
property to true
.
Note: The font must support scaling for this to work. See Scaling Fonts Automatically for more information.
RotatingLabel is available under the MIT license. See LICENSE for more info.