lexrus / LTMorphingLabel

[EXPERIMENTAL] Graceful morphing effects for UILabel written in Swift.
MIT License
8.09k stars 780 forks source link

LTMorphingLabel

Travis Language CocoaPods Carthage compatible Accio supported License

A morphing UILabel subclass written in Swift, originally designed to mimic Apple's QuickType animation in iOS 8 at WWDC 2014.

Table of Contents

Available Effects

.scale (default)

LTMorphingLabel

.evaporate

LTMorphingLabel-Evaporate

.fall

LTMorphingLabel-Fall

.pixelate

LTMorphingLabel-Pixelate

.sparkle

LTMorphingLabel-Sparkle

.sparkle is built on top of QuartzCore.CAEmitterLayer. There is also a SpriteKit-powered version here.

.burn

LTMorphingLabel-Burn

.anvil

LTMorphingLabel-Anvil

Usage

UIKit

Since LTMorphingLabel is a drop-in replacement, you can use it like any UILabel by setting its text property, yielding the default effect (.scale):

var exampleLabel = LTMorphingLabel()
exampleLabel.text = "This is a test"

UIKitExample1

Alternatively, it can be used interactively:

var exampleLabel = LTMorphingLabel()
exampleLabel.text = "This is a test"
exampleLabel.pause()

// Call .updateProgress(progress: Float) for interactive animation
// Note: In this case, animation will stop at 45% and will not complete
//       unless called later with 100 as the `progress` float value.
exampleLabel.updateProgress(progress: 45.0)

The effect can be changed by setting the morphingEffect property:

var exampleLabel = LTMorphingLabel()
exampleLabel.text = "This is a test"
exampleLabel.morphingEffect = .burn

UIKitExample2

SwiftUI

To use LTMorphingLabel in SwiftUI, simply declare it and set its text, effect, font, and textColor properties:

public var body: some View {
    VStack {
        MorphingText(
            "This is a test",
            font: UIFont.systemFont(ofSize: 20),
            textColor: .black,
            textAlignment: .center
        )
        .frame(maxWidth: 200, maxHeight: 100)
    }
}

Similar to its use in UIKit, you can also specify the morphing effect manually (if you do not want to use the default .scale effect):

public var body: some View {
    VStack {
        MorphingText(
            "This is a test",
            effect: .burn, // Specify an alternative morphing effect with this line
            font: UIFont.systemFont(ofSize: 20),
            textColor: .black,
            textAlignment: .center
        )
        .frame(maxWidth: 200, maxHeight: 100)
    }
}

LTMorphingLabelSwiftUI

Requirements

Installation

Swift Package Manager

Simply add this library to your package manifest or follow instructions on adding a package dependency using Xcode here.

.package(
    url: "https://github.com/lexrus/LTMorphingLabel.git",
    .branch("master")
)

CocoaPods

Add pod 'LTMorphingLabel' to your Podfile or follow instructions to add dependencies here.

Carthage

Add github "lexrus/LTMorphingLabel" to your Cartfile or follow instructions on adding frameworks here.

XCFramework

A pre-compiled xcframework file is available on the Releases page.

Accio

  1. Add this library to your package manifest (see Swift Package Manager)

  2. Update your target dependencies to include LTMorphingLabel:

    .target(
      name: "App",
      dependencies: [
          "LTMorphingLabel",
      ]
    ),
  3. Run accio update.

Unit Testing

Clone the repo by running git clone https://github.com/lexrus/LTMorphingLabel.git, then open the project with Xcode and press Cmd + U (or, in the menu bar, click Product > Build for > Testing).

Apps Using LTMorphingLabel

Third-Party Ports

Android

The Android port of this library is available here.

React Native

The React Native port of this library is available here.

License

This code is distributed under the terms and conditions of the MIT license.