UILabel (extension or subclass) that adjust the font size to fit a frame
UILabel
can shrink font size as follows:
label.minimumScaleFactor = 0.3
label.adjustsFontSizeToFitWidth = true
label.numberOfLines = 1
But it won’t always work as expected:
That’s why FittableFontLabel
exists:
maxFontSize
without using default label font sizeUILabel
extension if we want to use UILabel
FittableFontLabel
Multilines UILabel
:
Multilines UILabel
with attributed string (lines spacing):
Single line UILabel
:
Consistent font size across multiple labels FittableRootView
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.
To get a consistent font size across multiple labels embed your FittableFontLabels in a UIView
with the custom class FittableRootView
. Then give each label you want to keep consistent the same link identifer.
The FittableRootView acts as the root of a search for FittableFontLabels with link identifiers. Every FittableFontLabel with an identifier found by the search is updated to use the smallest auto adjusted font size calculated for that identifier.
Notes:
searchView
. This allows you to disable the search, giving the FittableRootView
identical behavior to a normal UIView
.Add pod FittableFontLabel
to your Podfile.
Add github "tbaranes/FittableFontLabel"
to your Cartfile.
FittableFontLabel is available on SPM. Just add the following to your Package file:
// swift-tools-version:5.5.0
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: CGFloat = 100,
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 100minFontScale
: 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 boundsfunc fontSizeThatFits(
text string: String,
maxFontSize: CGFloat = 100,
minFontScale: CGFloat = 0.1,
rectSize: CGSize? = nil) -> CGFloat
Returns the font size that can make the text
parameter fit the label frame.
text
: the text that needs to fit in the labelmaxFontSize
: the biggest font size that can be returned. The default value is 100minFontScale
: the scale factor that determines the smallest font size that can be returned. The default value is 0.1rectSize
: the size where the text must fit. The default value is the label boundsAn UILabel
subclass allowing you to automatize the process of adjusting the font size.
@IBInspectable public var autoAdjustFontSize: Bool = true
If true
, the font size will be adjusted each time that the text
or the frame
change.
@IBInspectable public var maxFontSize = CGFloat.NaN
The biggest font size to use during drawing. The default value is the current font size
@IBInspectable public var minFontScale = CGFloat.NaN
The scale factor that determines the smallest font size to use during drawing. The default value is 0.1
@IBInspectable public var leftInset: CGFloat = 0
@IBInspectable public var rightInset: CGFloat = 0
@IBInspectable public var topInset: CGFloat = 0
@IBInspectable public var bottomInset: CGFloat = 0
These four properties allow you to set a margin on your label. That will change the rect where the font must fit. The default value is 0.
FittableFontLabel
is available under the MIT license. See the LICENSE file for more info.