FittableFontLabel
FittableFontLabel is a custom UILabel class that automatically adjusts the font size to fit the available width. It is designed to solve the problem of text truncation or wrapping when the label’s text length exceeds the width of the label’s frame.
Installation
To install FittableFontLabel in your project, you can use CocoaPods. Simply add the following line to your Podfile:
pod ‘FittableFontLabel’
Then run the command pod install
to install the library.
Usage
Initialization
To create a FittableFontLabel instance, you can use Interface Builder or programmatically:
let label = FittableFontLabel(frame: CGRect(x: 0, y: 0, width: 200, height: 30))
Configuration
You can customize the following properties of FittableFontLabel:
- textColor: The color of the label’s text.
- font: The initial font for the label. The font size will be adjusted automatically.
- minimumFontSize: The smallest font size the label can shrink to.
- adjustsFontSizeToFitWidth: Whether the font size should be adjusted to fit the width. Set it to false if you want to manually handle text truncation.
Example
Here’s an example of using FittableFontLabel:
import UIKit
import FittableFontLabel
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let label = FittableFontLabel(frame: CGRect(x: 100, y: 100, width: 200, height: 30))
label.text = "This is a long text that will automatically adjust its font size to fit the width."
label.minimumFontSize = 12
label.adjustsFontSizeToFitWidth = true
self.view.addSubview(label)
}
}
Documentation
For more detailed information on using FittableFontLabel, you can refer to the API documentation.
Conclusion
FittableFontLabel is a convenient UILabel subclass that automatically adjusts the font size based on the available width. It helps eliminate text truncation or wrapping issues, providing a better user experience. Try it out in your project today!