UILabel can shrink font size as follows:
label.minimumScaleFactor = 0.3
label.adjustsFontSizeToFitWidth = true
label.numberOfLines = 1But it won't always work as expected:
- Doesn't fit the label height
- Big top / bottom margins when the maxFontSize is huge
- Not really customisable
- ...
That's why FittableFontLabel exists:
- Make the text fit the label size: width and height if multilines, width only if single lines
- Works with one or several lines
- Supports attributed string (custom line spacing...)
- Customize
maxFontSizewithout using default label font size - Auto-layout compliant
UILabelextension if we want to useUILabel- Customisable from xibs / storyboards when using the UILabel's subclass
FittableFontLabel - ...
Multilines UILabel:
Multilines UILabel with attributed string (lines spacing):
Single line UILabel:
let aFittableFontLabel = FittableFontLabel(frame: CGRect(x: 0, y: 0, width: 300, height: 100))
aFittableFontLabel.autoFittableFont = true
aFittableFontLabel.lineBreakMode = .ByWordWrapping
aFittableFontLabel.numberOfLines = 0 // or 1...
aFittableFontLabel.text = "?"
// Change the text, it will always fit the label frame!Check the sample project for advanced usage.
Note: The label lineBreakMode must be set to NSLineBreakByWordWrapping in order to work.
- iOS 8.0+
FittableFontLabel is available on CocoaPods. Just add the following to your Podfile:
pod 'FittableFontLabel'
FittableFontLabel is available on SPM. Just add the following to your Package file:
import PackageDescription
let package = Package(
dependencies: [
.Package(url: "https://github.com/tbaranes/FittableFontLabel.git", majorVersion: 1)
]
)Just drag the Source/*.swift files into your project.
func fontSizeToFit(
maxFontSize maxFontSize: CGFloat = CGFloat.NaN,
minFontScale: CGFloat = 0.1,
rectSize: CGSize? = nil)Adjust the font size to make the current text fit the label frame.
maxFontSize: the biggest font size to use during drawing. The default value is the current font sizeminFontScale: the scale factor that determines the smallest font size to use during drawing. The default value is 0.1rectSize: the size where the text must fit. The default value is the label bounds
An UILabel subclass allowing you to automatize the process of adjusting the font size.
@IBInspectable public var autoAdjustFontSize: Bool = trueIf true, the font size will be adjusted each time that the text or the frame change.
@IBInspectable public var maxFontSize = CGFloat.NaNThe biggest font size to use during drawing. The default value is the current font size
@IBInspectable public var minFontScale = CGFloat.NaNThe scale factor that determines the smallest font size to use during drawing. The default value is 0.1
- Binary search to improve the performance
- Tests
- Your features!
- If you found a bug, open an issue
- If you have a feature request, open an issue
- If you want to contribute, submit a pull request
FittableFontLabel is available under the MIT license. See the LICENSE file for more info.


