• Stars
    star
    108
  • Rank 321,201 (Top 7 %)
  • Language
    Swift
  • Created almost 2 years ago
  • Updated about 1 year ago

Reviews

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

Repository Details

Attributed String Builders

AttributedString Builder

A simple way to build up attributed strings using result builders from a variety of sources. Based on the episodes from Swift Talk. Here are the things you can embed:

  • Plain strings
  • Markdown
  • Images
  • SwiftUI Views
  • Table support
  • Multi-page PDF export
  • Footnotes

Here's an example showing plain strings, Markdown and SwiftUI views:

@AttributedStringBuilder
var example: some AttributedStringConvertible {
    "Hello, World!"
        .bold()
        .modify { $0.backgroundColor = .yellow }
    """
    This is some markdown with **strong** `code` text. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas tempus, tortor eu maximus gravida, ante diam fermentum magna, in gravida ex tellus ac purus.

    - One
    - Two
    - Three

    > A blockquote.
    """.markdown()
    Embed {
        HStack {
            Image(systemName: "hand.wave")
                .font(.largeTitle)
            Text("Hello from SwiftUI")
            Color.red.frame(width: 100, height: 50)
        }
    }

You can then turn this example into a multi-page PDF like this:

let data = await example
    .joined(separator: "\n") // join the parts using newlines
    .run(environment: .init(attributes: sampleAttributes)) // turn into a single `NSAttributedString`
    .pdf() // render as PDF
try! data.write(to: .desktopDirectory.appending(component: "out.pdf"))

Here's a larger sample.

Features

Attributes

The Attributes struct is a value type representing the attributes in an NSAttributedString. During the building of the attributed string, this is passed on through the environment. For example, this is how you can build a simple attributed string using plain strings and .modify:

@AttributedStringBuilder var sample1: some AttributedStringConvertible {
    "Hello"
    "World".modify { $0.textColor = .red }
}

Strings

You can turn any string directly into an attributed string. The attributes from the environment are used to do this. You can also modify the environment in a way very similar to what SwiftUI does. For example, you can write "Hello".bold()" to take the current attributes, make them bold, and then render the string "Hello"` using these modified attributes.

Markdown

You can take any Markdown string and render it into an attributed string as well. For most customization, you can pass in a custom stylesheet. In the Markdown string literal, you can embed other values that convert to AttributedStringConvertible:

@AttributedStringBuilder var sample2: some AttributedStringConvertible {
    Markdown("""
    This is *Markdown* syntax.

    With \("inline".modify { $0.underlineStyle = .single }) nesting.
    """)
}

Images

You can embed any NSImage into the attributed string, they're rendered as-is.

SwiftUI Views

SwiftUI views can be embedded using the Embed modifier. By default, it proposes nil⨉nil to the view, but this can be customized. SwiftUI views are rendered into a PDF context and are embedded as vector graphics.

Tables

You can construct tables in attributed strings using the Table support. This interface might still change (ideally, we'd use result builders for this as well).

Environment

You can use the environment in a way similar to SwiftUI's Environment to pass values down the tree.

State

Similar to the environment, you can also thread state through. This is useful (for example) to number footnotes. While the modified environment is always passed to children of the current node, modified state is passed to the next nodes that are rendered.

Swift Talk Episodes

More Repositories

1

app-architecture

Sample Code of the App Architecture Book
Swift
2,021
star
2

functional-swift

Issue repository for the Functional Swift book
Swift
927
star
3

issue-13-viper

Objective-C
846
star
4

core-data

Sample code for the objc.io Core Data book
Swift
769
star
5

issue-1-lighter-view-controllers

Example project for the 1st issue of objc.io
Objective-C
740
star
6

articles

All current objc.io articles
593
star
7

markdown-playgrounds

A Markdown Editor that can execute Swift code
Swift
575
star
8

thinking-in-swiftui-sample-code

Sample code for the Thinking in SwiftUI book
Swift
318
star
9

swift-talk-backend

Swift
287
star
10

tiny-networking

Tiny Networking Library
Swift
257
star
11

OptimizingCollections

Home of my talk about Optimizing Collections in Swift
Swift
233
star
12

issue-21-core-image-explorer

Sample code for the Introduction to Core Image article in objc.io issue #21
Swift
216
star
13

issue-3-collection-view-layouts

Objective-C
194
star
14

issue-4-full-core-data-application

Objective-C
164
star
15

issue5-view-controller-transitions

Simple demo app showing custom view controller transitions
Objective-C
124
star
16

issue-2-background-core-data

Objective-C
89
star
17

issue-10-core-data-network-application

Sample project for objc.io article http://www.objc.io/issue-10/networked-core-data-application.html
Objective-C
88
star
18

issue-7-lab-color-space-explorer

Objective-C
74
star
19

issue-12-custom-layer-property-animations

Issue 12: Custom Layer Property Animations
Objective-C
66
star
20

issue-3-auto-layout-debugging

Objective-C
62
star
21

issue-14-xpc

Sample Project for objc.o Article about XPC
Swift
55
star
22

LedgerGUI

Swift
55
star
23

issue-12-interactive-animations-pop

Objective-C
50
star
24

S01E51-playground-driven-development-at-kickstarter

Sample code for Swift Talk episode 51: Playground-Driven Development at Kickstarter
Swift
48
star
25

S01E01-networking

Sample code for Swift Talk episode 1: Networking
Swift
46
star
26

issue-12-interactive-animations-uidynamics

Interactive Animations with UIDynamics
Objective-C
45
star
27

swift-talk-app-swiftui

Swift Talk App using SwiftUI
Swift
45
star
28

swiftui-challenges

SwiftUI challenges
41
star
29

issue-12-interactive-animations

Interactive Animations on iOS
Objective-C
41
star
30

issue-2-background-networking

Objective-C
40
star
31

issue-4-importing-and-fetching

Objective-C
39
star
32

keychain-item

Keychain Item Property Wrapper
Swift
38
star
33

StaticSite

Simple and blunt static site generator
Swift
37
star
34

issue-2-background-file-io

Objective-C
34
star
35

metal-demo-objcio

Sample code for Issue #18 of objc.io
Objective-C
33
star
36

issue-7-contact-editor

Objective-C
30
star
37

issue-8-quadcopter-navigator

Objective-C
30
star
38

S01E34-reactive-programming

Sample code for Swift Talk episode 34: Reactive Programming
Swift
25
star
39

issue-14-scriptable-apps

Objective-C
24
star
40

thinking-in-swiftui-code

Accompanying code for the book Thinking in SwiftUI
24
star
41

S01E03-loading-view-controllers

Sample code for Swift Talk episode 3
Swift
23
star
42

advanced-swift

Feedback and issue-tracker for objc.io's Advanced Swift book
23
star
43

S01E53-test-driven-reactive-programming-at-kickstarer

Sample code for Swift Talk episode 53: Test-Driven Reactive Programming at Kickstarter
Swift
21
star
44

S01E90-concurrent-map

Sample code for Swift Talk episode 90: Concurrent Map
Swift
19
star
45

S01E25-network-caching

Sample code for Swift Talk episode 25: Network Caching
Swift
18
star
46

issue-7-linguistic-tagging

Objective-C
18
star
47

S01E05-connecting-view-controllers

Sample code for Swift Talk Episode #5: Connecting View Controllers
Swift
17
star
48

S01E06-generic-table-view-controllers

Sample code for Swift Talk Episode #6: Generic Table View Controllers
Swift
16
star
49

issue-6-compiler-tool

Example code for a standalone clang/llvm tool.
C++
16
star
50

S01E26-generic-table-view-controllers-part-2

Sample code for Swift Talk episode 26: Generic Table View Controllers (Part 2)
Swift
15
star
51

collection-view-swiftui

Sample code for Swift Talk episode 168: SwiftUI — Building a Collection View (Part 2)
Swift
15
star
52

thinking-in-swiftui-qa

15
star
53

issue-16-functional-apis

Example playground for the article http://www.objc.io/issue16/functional-swift-apis.html
Swift
13
star
54

s01e47-testable-view-models-at-kickstarter

Sample code for Swift Talk episode 47: View Models at Kickstarter
Swift
12
star
55

swift-talk-shared

Swift
11
star
56

issue-12-interactive-animations-osx

Objective-C
11
star
57

S01E57-certificate-pinning

Sample code for Swift Talk episode 57: Certificate Pinning
Swift
10
star
58

S01E43-view-controller-refactoring

Swift
10
star
59

S01E02-rendering-commonmark

Sample code for Swift Talk episode 2: Rendering CommonMark
Swift
10
star
60

S01E62-testable-view-controllers-with-reducers

Sample code for Swift Talk episode 62: Testable View Controllers with Reducers
Swift
10
star
61

S01E145-setting-up-a-document-based-app

Sample code for Swift Talk episode 145: Markdown Playgrounds — Setting Up a Document Based App
Swift
10
star
62

issue-9-string-parsing

Objective-C
9
star
63

S01E75-auto-layout-with-key-paths

Sample code for Swift Talk episode 75: Auto Layout with Key Paths
Swift
9
star
64

S01E49-deeplinking-at-kickstarter

Swift
8
star
65

S01E133-tiny-networking-library-revisited

Sample code for Swift Talk episode 133: Networking: Tiny Networking Library Revisited
Swift
8
star
66

S01E174-animation-curves

Sample code for Swift Talk episode 174: SwiftUI — Animation Curves
Swift
7
star
67

S01E07-stack-views-with-enums

Sample code for Swift Talk episode #7: Stack Views with Enums
Swift
7
star
68

issue-14-plugins

Objective-C
7
star
69

S01E117-showing-hiding-sections

Sample code for Swift Talk episode 117: Building a Form Library: Showing & Hiding Sections
Swift
7
star
70

S01E08-networking-post-requests

Sample code for Swift Talk episode 8: Networking: POST Requests
Swift
7
star
71

S01E27-typed-notifications-part-1

Sample code for Swift Talk episode 27: Typed Notifications (Part 1)
Swift
6
star
72

DynamicLayout

Swift
6
star
73

S01E109-ios-remote-debugger-connecting-with-bonjour

Sample code for Swift Talk episode 109: iOS Remote Debugger: Connecting with Bonjour
Swift
6
star
74

S01E253-flow-layout-revisited

Sample code for Swift Talk 253: SwiftUI – Flow Layout Revisited
Swift
6
star
75

S01E225-swiftui-layout-explained-view-protocols-and-shapes

Sample code for Swift Talk 225: SwiftUI Layout Explained – View Protocols and Shapes
Swift
6
star
76

S01E333-sticky-headers-for-scroll-views

Sample code for Swift Talk 333: Sticky Headers for Scroll Views
Swift
6
star
77

S01E73-view-bindings-in-pure-swift

Sample code for Swift Talk episode 73: View Bindings in Pure Swift
Swift
6
star
78

S01E71-type-safe-file-paths-with-phantom-types

Sample code for Swift Talk episode 71: Type-Safe File Paths with Phantom Types
Swift
5
star
79

S01E66-the-elm-architecture-part-1

Sample code for Swift Talk episode 66: The Elm Architecture (Part 1)
Swift
5
star
80

S01E45-server-side-swift-routing

Swift
5
star
81

S01E28-typed-notifications-part-2

Sample code for Swift Talk episode 28: Typed Notifications (Part 2)
Swift
5
star
82

retro-rampage

Swift
5
star
83

S01E235-swiftui-layout-explained-layout-priorities

Sample code for Swift Talk 235: SwiftUI Layout Explained – Layout Priorities
Swift
5
star
84

S01E19-from-runtime-programming-to-functions

Sample code for Swift Talk episode 19: From Runtime Programming to Functions
Swift
5
star
85

S01E04-rendering-commonmark-part-2

Swift Talk Episode 4: Rendering CommonMark (Part 2)
Swift
5
star
86

S01E175-building-a-shopping-cart-animation

Sample code for Swift Talk episode 175: SwiftUI — Building a Shopping Cart Animation
Swift
5
star
87

libpq

Swift
4
star
88

S01E29-protocols-class-hierarchies

Sample code for Swift Talk episode 29: Protocols & Class Hierarchies
Swift
4
star
89

S01E156-a-first-look-at-swiftui

Sample code for Swift Talk episode 156: SwiftUI — A First Look
Swift
4
star
90

S01E149-swift-syntax-highlighting

Sample code for Swift Talk episode 149: Markdown Playgrounds — Swift Syntax Highlighting
Swift
4
star
91

S01E179-building-a-shopping-cart-cleanup-and-refactoring

Sample code for Swift Talk episode 179: SwiftUI — Building a Shopping Cart — Cleanup & Refactoring
Swift
4
star
92

S01E229-swiftui-layout-explained-flexible-frames

Sample code for Swift Talk 229: SwiftUI Layout Explained – Flexible Frames
Swift
4
star
93

S01E86-sharing-state-between-view-controllers-in-mvc-part-1

Sample code for Swift Talk episode 86: Sharing State Between View Controllers in MVC (Part 1)
Swift
4
star
94

S01E168-building-a-collection-view-part-2

Sample code for Swift Talk episode 168: SwiftUI — Building a Collection View (Part 2)
Swift
4
star
95

S01E406-detecting-visible-cells

Sample code for Swift Talk 406: Detecting Visible Cells
Swift
4
star
96

S01E36-futures

Sample Code for Swift Talk Episode 36: Futures
Swift
4
star
97

S01E211-quick-open-simple-fuzzy-matching

Sample code for Swift Talk 211: Quick Open — Simple Fuzzy Matching
Swift
4
star
98

S01E183-lap-times

Sample code for Swift Talk episode 183: SwiftUI Stopwatch — Lap Times
Swift
3
star
99

S01E137-testing-networking-code

Sample code for Swift Talk episode 136: Networking — Testing Networking Code
Swift
3
star
100

S01E63-mutable-shared-structs-part-2

Sample code for Swift Talk episode 63: Mutable Shared Structs (Part 2)
Swift
3
star