Swiftpack.co -  Iaenhaall/AttributedText as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
Iaenhaall/AttributedText
AttributedText is a view for displaying some HTML-tagged text using SwiftUI Text View.
.package(url: "https://github.com/Iaenhaall/AttributedText.git", from: "v1.1.0")

AttributedText

AttributedText is a view for displaying some HTML-tagged text using SwiftUI Text View.

A simple example of usage.

Code example:

AttributedText("A unit of <i>length</i> equal to <b>one hundred-millionth of a centimetre</b>, 10<sup>−10</sup> metre, used mainly to express <u>wavelengths and interatomic distances</u>.")

Result:

Example

Features

You can clone the repo and run the AttributedTextExample project to explore the AttributedText features.

These are the main points to pay attention to.

  1. You can define the tags you need or use the defaults.

    You need to set the required tags and provide associated closures. Each closure must be a modifier that is applied to the SwiftUI Text View when a specific tag is encountered.

    Override the default values, for example at application launch.
    @main
    struct ExampleApp: App {
        init() {
            AttributedText.tags = [
                "b": { $0.bold() },
                "i": { $0.italic() }
            ]
        }
    }
    

    In this case only <b> and <i> tags will be processed. All other tags will be ignored or deleted.

    Set the custom values for each instance.
    private let tags: Dictionary<String, (Text) -> (Text)> = [
        // Set the necessary values.
    ]
    
    var body: some View {
        AttributedText("Text", tags: tags)
    }
    
  2. Basic modifiers can still be applied, such as changing the font and color of the text.

    Code example:

    AttributedText("This is <b>bold</b> and <i>italic</i> text.")
        .foregroundColor(.blue)
        .font(.title)
    

    Result:

    1 feature

  3. Handles unopened/unclosed tags.

    Code example:

    AttributedText("This is italic</i> and <b>bold text.")
    

    Result:

    2 feature

  4. Supports overlapping tags.

    Code example:

    AttributedText("This is <b>bold only, <i>bold and italic</b> and italic only</i> text.")
    

    Result:

    3 feature

  5. Deletes tags that have no modifiers.

    Code example:

    AttributedText("<unknown>This is <b>bold</b> and <i>italic</i> text.</unknown>")
    

    Result:

    4 feature

  6. Does not handle HTML characters such as &amp;.

    Code example:

    AttributedText("This is <b>bold</b> &amp; <i>italic</i> text.")
    

    Result:

    5 feature

  7. Only single-word tags are supported. Tags with more than one word or containing any characters besides letters or numbers are ignored and not removed.

    Code example:

    AttributedText("This is <tag attribute1=\"value1\"> <b>bold</b> and <i>italic</i> text</tag>.")
    

    Result:

    6 feature

Installation and usage

Via Swift Package Manager

  1. In Xcode 11 or greater select File ▸ Swift Packages ▸ Add Package Dependency.
  2. Paste the link to this repo https://github.com/Iaenhaall/AttributedText.git and click Next.
  3. Define the package options for this package or select the default. Click Next.
  4. Xcode downloads the code from GitHub and adds the package to the your project target. Click Finish.

Manually

  1. Add AttributedText.swift and HTML2TextParser.swift files to your project.

GitHub

link
Stars: 4
Last commit: 6 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.

Release Notes

1.2.0
6 weeks ago

Added the ability to set custom tags.

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