• This repository has been archived on 23/Jan/2023
  • Stars
    star
    122
  • Rank 292,031 (Top 6 %)
  • Language
    Swift
  • License
    MIT License
  • Created almost 4 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Render attributed strings in SwiftUI

Warning

This repo has been archived.

From macOS 12+ / iOS 15+ / tvOS 15+ / watchOS 8+, you can use AttributedString with the SwiftUI Text view.

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

More Repositories

1

swift-markdown-ui

Display and customize Markdown text in SwiftUI
Swift
2,372
star
2

Groot

From JSON to Core Data and back.
Objective-C
534
star
3

Vertigo

Simple full screen image viewer with image zoom custom view controller transition
Objective-C
355
star
4

DefaultCodable

A convenient way to handle default values with Swift Codable types
Swift
353
star
5

Markup

Lightweight markup text formatting in Swift
Swift
95
star
6

AdaptiveCardUI

Snippets of UI, authored in JSON and rendered with SwiftUI
Swift
92
star
7

ReadingList

An example on using the Mantle Modeling Framework with Overcoat AFNetworking extension.
Objective-C
86
star
8

NetworkImage

Asynchronous image loading in SwiftUI
Swift
69
star
9

SimpleNetworking

Scalable and composable API Clients using Swift Combine
Swift
51
star
10

IndeterminateTypesWithCodable

Indeterminate Types with Codable in Swift
Swift
37
star
11

Borders

Sample code for Consuming Web Services with Swift and ReactiveX
Swift
33
star
12

TGRDataSource

Convenience UITableView and UICollectionView data sources
Objective-C
24
star
13

SwiftCommonMark

Parse and create CommonMark documents in Swift.
C
23
star
14

Gig

A great Twitter API client for Objective-C
Objective-C
19
star
15

Reusable

iOS cell registration and reusing with generics and protocol extensions
Swift
13
star
16

DirectLine

Swift client library for Microsoft Bot Framework's Direct Line protocol
Swift
11
star
17

rxswift-gentle-introduction

A gentle introduction to RxSwift http://www.vsmobile.tech
7
star
18

swift-any-value

A Swift Codable type that serves as a placeholder for any JSON value
Swift
3
star
19

NSSpain2016

From Design to Code
3
star
20

TwitterTimeline

Source code for my article 'Implementando una timeline de Twitter con Core Data'
Objective-C
2
star