AnimateText Documentation

repository·main·Indexed 19 days ago

https://github.com/jasudev/animatetext

A SwiftUI library for iOS and macOS that provides customizable text animations by splitting strings into units such as letters or words and applying individual ViewModifiers via the ATTextAnimateEffect protocol.

Tokens
950
Snippets
4
Records
4
Agent score
15%

What's inside AnimateText

  1. Install AnimateText via Swift Package Manager

    main

    To add AnimateText to your project, add the following dependency to your Package.swift file. This project is currently available on the main branch.

    dependencies: [
        .package(url: "https://github.com/jasudev/AnimateText.git", .branch("main"))
    ]
  2. Implement a custom ATTextAnimateEffect

    main

    To create a custom animation, implement the ATTextAnimateEffect protocol. This protocol inherits from ViewModifier, allowing you to apply animations to the individual text elements (characters, words, etc.) generated by the library.

    Your implementation must include:

    1. data: An ATElementData object containing the element's index and value.
    2. userInfo: An optional Any? object for custom state.
    3. An initializer: init(_ data: ATElementData, _ userInfo: Any?).
    4. The body(content:) method: Where you define the actual SwiftUI animation logic applied to the content.
    /// Custom animation effect.
    public struct CustomEffect: ATTextAnimateEffect {
    
        public var data: ATElementData
        public var userInfo: Any?
    
        public init(_ data: ATElementData, _ userInfo: Any?) {
            self.data = data
            self.userInfo = userInfo
        }
    
        public func body(content: Content) -> some View {
            content
                .opacity(data.value)
                .animation(.easeInOut.delay(Double(data.index) * 0.06), value: data.value)
        }
    }
  3. Use the AnimateText view

    main

    The AnimateText<E> view animates a binding to a string. You must specify the effect type E as a generic, where E conforms to ATTextAnimateEffect.

    Key parameters:

    • $text: A Binding<String> to the text you want to animate.
    • type: An ATUnitType (e.g., .letters) that determines how the text is split for animation.
    • userInfo: An optional Any? object used to pass custom data to the effect.
    @State var text: String = "AnimateText"
    @State var type: ATUnitType = .letters
    @State var userInfo: Any? = nil
    
    AnimateText<CustomEffect>($text, type: type, userInfo: userInfo)
  4. Reference the ATTextAnimateEffect protocol

    main

    The ATTextAnimateEffect protocol is the core interface for defining how text elements are animated. It is a ViewModifier.

    /// A protocol to implement text animation effects.
    public protocol ATTextAnimateEffect: ViewModifier {
    
        /// Informational data required for each element animation.
        var data: ATElementData { get }
        /// Custom user info for the effect.
        /// The effect maintains a strong reference to this object until it (the effect) is invalidated. This parameter may be nil.
        var userInfo: Any? { get }
    
        init(_ data: ATElementData, _ userInfo: Any?)
    }