Swiftpack.co - gonzalezreal/AttributedText as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
gonzalezreal/AttributedText
Render attributed strings in SwiftUI
.package(url: "https://github.com/gonzalezreal/AttributedText.git", from: "0.3.1")

AttributedText

Twitter: @gonzalezreal

AttributedText is a Swift µpackage that provides NSAttributedString rendering in SwiftUI by wrapping either an NSTextView or a UITextView depending on the platform.

Supported Platforms

  • macOS 11.0+
  • iOS 14.0+
  • tvOS 14.0+

Usage

import AttributedText

struct ContentView: View {
    private let attributedString: NSAttributedString = {
        let result = NSMutableAttributedString(
            string: """
            After the Big Bang
            A brief summary of time
            Life on earth
            10 billion years
            You reading this
            13.7 billion years
            """
        )

        result.addAttributes([.font: UIFont.preferredFont(forTextStyle: .title1)], range: NSRange(location: 0, length: 18))
        result.addAttributes([.link: URL(string: "https://en.wikipedia.org/wiki/Big_Bang")!], range: NSRange(location: 10, length: 8))
        result.addAttributes([.font: UIFont.preferredFont(forTextStyle: .body)], range: NSRange(location: 19, length: 23))
        result.addAttributes([.font: UIFont.preferredFont(forTextStyle: .title2)], range: NSRange(location: 43, length: 13))
        result.addAttributes([.font: UIFont.preferredFont(forTextStyle: .body)], range: NSRange(location: 57, length: 16))
        result.addAttributes([.font: UIFont.preferredFont(forTextStyle: .title2)], range: NSRange(location: 74, length: 16))
        result.addAttributes([.font: UIFont.preferredFont(forTextStyle: .body)], range: NSRange(location: 91, length: 18))

        return result
    }()

    var body: some View {
        AttributedText(attributedString)
            .background(Color.gray.opacity(0.5))
            .accentColor(.purple)
    }
}

iOSScreenshot

An AttributedText view takes all the available width and adjusts its height to fit the contents.

To change the text alignment or line break mode, you need to add a .paragraphStyle attribute to the attributed string.

Installation

You can add AttributedText to an Xcode project by adding it as a package dependency.

  1. From the File menu, select Swift Packages › Add Package Dependency…
  2. Enter https://github.com/gonzalezreal/AttributedText into the package repository URL text field
  3. Link AttributedText to your application target

GitHub

link
Stars: 63
Last commit: 4 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Release Notes

AttributedText 0.3.1
4 weeks ago
  • Set platform requirements and remove playgrounds

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics