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

Last update: Jun 9, 2022

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
import SwiftUI

struct ContentView: View {
  var body: some View {
    AttributedText {
      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
    }
    .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

https://github.com/mobilite-src/AttributedText
You might also like...

Twitter Text Libraries. This code is used at Twitter to tokenize and parse text to meet the expectations for what can be used on the platform.

twitter-text This repository is a collection of libraries and conformance tests to standardize parsing of Tweet text. It synchronizes development, tes

Jun 28, 2022

A TextView that provides easy to use tagging feature for Mention or Hashtag

A TextView that provides easy to use tagging feature for Mention or Hashtag

Tagging A TextView that provides easy to use tagging feature for Mention or Hashtag. Introduction Tagging is a UIView that encloses a TextView that co

Jun 9, 2022

A standalone, flexible API that provides a full-featured rich text editor for iOS applications.

A standalone, flexible API that provides a full-featured rich text editor for iOS applications.

Twitter Text Editor A standalone, flexible API that provides a full featured rich text editor for iOS applications. This provides a robust text attrib

Jun 24, 2022

A simple library that provides standard Unicode emoji support across all platforms

Twitter Emoji (Twemoji) A simple library that provides standard Unicode emoji support across all platforms. Twemoji v13.1 adheres to the Unicode 13.0

Jun 20, 2022

Like a SwiftUI ViewBuilder, but for Text

Like a SwiftUI ViewBuilder, but for Text

TextBuilder Introduction Text composition in SwiftUI can often be cumbersome, especially when there's logic affecting its format and content. TextBuil

May 17, 2022

Markdown in SwiftUI, and some other interesting components.

Markdown in SwiftUI, and some other interesting components.

RoomTime RoomTime is a bundle of tools developed in my app RoomTime Lite. ( 😊 RoomTime Lite is still in development) Features TextArea AutoWrap Markd

Jun 18, 2022

A lightning fast, native SwiftUI scratchpad/text editor.

Sedit A lightning fast, native SwiftUI scratchpad/text editor. Sedit (Swift Edit, as in the language and as in fast) is a lightning fast basic text ed

Jan 28, 2022

Easily show RichText(html) in SwiftUI

Easily show RichText(html) in SwiftUI

RichText LightMode DarkMode Code import SwiftUI

Jun 19, 2022

SensibleStyling - Helpers for sensible SwiftUI styling

SensibleStyling - Helpers for sensible SwiftUI styling

SensibleStyling Helpers for sensible SwiftUI styling. License See the LICENSE fi

Feb 11, 2022
Related tags
Convert text with HTML tags, links, hashtags, mentions into NSAttributedString. Make them clickable with UILabel drop-in replacement.
Convert text with HTML tags, links, hashtags, mentions into NSAttributedString. Make them clickable with UILabel drop-in replacement.

Atributika is an easy and painless way to build NSAttributedString. It is able to detect HTML-like tags, links, phone numbers, hashtags, any regex or

Jun 20, 2022
Programmatic creation of NSAttributedString doesn't have to be a pain
Programmatic creation of NSAttributedString doesn't have to be a pain

Tribute let string = NSMutableAttributedString().add("Hello ") { $0.font = .systemFontOfSize(20) $0.color = .redColor() $0.underline = .St

May 31, 2022
Type strict builder class for NSAttributedString.
Type strict builder class for NSAttributedString.

StringStylizer Type strict builder class for NSAttributedString. What's this? StringStylizer makes NSAttributedString more intuitive with Method chain

Jan 12, 2021
LDOMarkdownParser - Parse (some) markdown attributes into an NSAttributedString

LDOMarkdownParser Description Convert markdown text styling into an NSAttributed

Feb 4, 2022
Blazing fast Markdown / CommonMark rendering in Swift, built upon cmark.
Blazing fast Markdown / CommonMark rendering in Swift, built upon cmark.

Down Blazing fast Markdown (CommonMark) rendering in Swift, built upon cmark v0.29.0. Is your app using it? Let us know! If you're looking for iwasrob

Jun 22, 2022
Markdown parsing and rendering for iOS and OS X
Markdown parsing and rendering for iOS and OS X

CocoaMarkdown Markdown parsing and rendering for iOS and macOS CocoaMarkdown is a cross-platform framework for parsing and rendering Markdown, built o

Jun 17, 2022
OysterKit is a framework that provides a native Swift scanning, lexical analysis, and parsing capabilities. In addition it provides a language that can be used to rapidly define the rules used by OysterKit called STLR

OysterKit A Swift Framework for Tokenizing, Parsing, and Interpreting Languages OysterKit enables native Swift scanning, lexical analysis, and parsing

Jun 10, 2022
Easiest way to create an attributed UITextView (with support for multiple links and from html)
Easiest way to create an attributed UITextView (with support for multiple links and from html)

AttributedTextView Easiest way to create an attributed UITextView (with support for multiple links and html). See the demo app and the playground for

May 3, 2022
An easy way to add mentions to uitextview like Facebook and Instagram
An easy way to add mentions to uitextview like Facebook and Instagram

OEMentions An easy way to add mentions to uitextview like Facebook and Instagram. It also include a tableview to show the users list to choose from. T

Apr 8, 2022
A Cross-Platform String and Regular Expression Library written in Swift.

Guitar ?? A Cross-Platform String and Regular Expression Library written in Swift. About This library seeks to add common string manipulation function

May 29, 2022