EvenlyWrappedLabel
is a UILabel
subclass that will vertically distribute text or attributed text evenly across any number of lines, preventing text from grouping up at the top and also preventing single word orphans from taking up the entire bottom line.
Examples:
-
This text:
This sentence has a lot of words on the top line.
Becomes:
This sentence has a lot of words on the top line.
-
This text:
This sentence has a single orphan.
Becomes:
This sentence has a single orphan.
Works with iOS 9.0+ and Xcode 9.0.0+.
- Works with all devices running iOS 9+.
- Uses
drawText(in:)
internally (as opposed to creating subviews), so anyUILabel
reference can be replaced withEvenlyWrappedLabel
and Everything Will Just Work™. - Works with any number of lines, any text alignment and attributed text.
- Works with autolayout and honors intrinsic sizing.
- Works with labels created in code and in Interface Builder.
-
useEveryLine
option to force shorter text to take up all available lines. - Example app with lots of real-time configurations that lets you see the text wrap as you type.
Swap any UILabel
reference with EvenlyWrappedLabel
:
class ViewController {
let label = UILabel()
}
import EvenlyWrappedLabel
class ViewController {
let label = EvenlyWrappedLabel()
}
Set label.useEveryLine = true
to spread the text across all numberOfLines
, even if the text isn't long enough to do so by default.
Example:
-
When
numberOfLines
=3
, anduseEveryLine
=true
, the following text:This only takes up one line.
Becomes:
This only takes up one line.
CocoaPods is a dependency manager for Cocoa projects.
CocoaPods 0.36 adds supports for Swift and embedded frameworks. You can install it with the following command:
$ gem install cocoapods
To integrate EvenlyWrappedLabel
into your Xcode project using CocoaPods, specify it in your Podfile
:
pod 'EvenlyWrappedLabel'
Then, run the following command:
$ pod install
In case Xcode complains ("Cannot load underlying module for EvenlyWrappedLabel") go to Product and choose Clean (or simply press ⇧⌘K).
If you prefer not to use CocoaPods, you can integrate EvenlyWrappedLabel
into your project manually.
EvenlyWrappedLabel
is developed by Jeff Burt at StockX and is released under the MIT license. See the LICENSE
file for details.
Feel free to follow me on my personal Twitter account. If you find any problems with the project or have ideas to enhance it, feel free to open a GitHub issue and/or create a pull request.