Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.
/ EvenlyWrappedLabel Public archive

EvenlyWrappedLabel vertically distributes text evenly across any number of lines, preventing orphans and text from grouping up at the top.

License

Notifications You must be signed in to change notification settings

stockx/EvenlyWrappedLabel

Repository files navigation

EvenlyWrappedLabel Logo

Swift4.0 Platform Version License

Description

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:

  1. 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.
    
  2. 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+.

Contents

  1. Features
  2. Usage
  3. Installation
  4. License
  5. Contact
  • Works with all devices running iOS 9+.
  • Uses drawText(in:) internally (as opposed to creating subviews), so any UILabel reference can be replaced with EvenlyWrappedLabel 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.

Example Project Screenshot

Set up

Swap any UILabel reference with EvenlyWrappedLabel:

In code


Before:
class ViewController {
    let label = UILabel()
}
After:
import EvenlyWrappedLabel

class ViewController {
    let label = EvenlyWrappedLabel()
}

In Interface Builder


Before:

Interface Builder Screenshot - Before

After:

Interface Builder Screenshot - After

(Optional) Use Every Line

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, and useEveryLine = true, the following text:

      This only takes up one line.
    

    Becomes:

      This only
      takes up
      one line.
    

CocoaPods

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).

Manually

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.

About

EvenlyWrappedLabel vertically distributes text evenly across any number of lines, preventing orphans and text from grouping up at the top.

Resources

License

Stars

Watchers

Forks

Packages

No packages published