Skip to content

Commit

Permalink
Add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzalezreal committed Dec 18, 2020
1 parent bef5a99 commit 2adf1b8
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 80 deletions.
5 changes: 0 additions & 5 deletions .spi.yml

This file was deleted.

47 changes: 23 additions & 24 deletions Playgrounds/AttributedText_iOS.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
import AttributedText
import PlaygroundSupport
import SwiftUI

import AttributedText

func makeAttributedString() -> 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
"""
)
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))
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
}
return result
}()

struct ContentView: View {
var body: some View {
AttributedText(makeAttributedString())
AttributedText(attributedString)
.background(Color.gray.opacity(0.5))
.accentColor(.purple)
}
Expand Down
47 changes: 23 additions & 24 deletions Playgrounds/AttributedText_macOS.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
import AttributedText
import PlaygroundSupport
import SwiftUI

import AttributedText

func makeAttributedString() -> 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
"""
)
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: NSFont.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: NSFont.preferredFont(forTextStyle: .body)], range: NSRange(location: 19, length: 23))
result.addAttributes([.font: NSFont.preferredFont(forTextStyle: .title2)], range: NSRange(location: 43, length: 13))
result.addAttributes([.font: NSFont.preferredFont(forTextStyle: .body)], range: NSRange(location: 57, length: 16))
result.addAttributes([.font: NSFont.preferredFont(forTextStyle: .title2)], range: NSRange(location: 74, length: 16))
result.addAttributes([.font: NSFont.preferredFont(forTextStyle: .body)], range: NSRange(location: 91, length: 18))
result.addAttributes([.font: NSFont.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: NSFont.preferredFont(forTextStyle: .body)], range: NSRange(location: 19, length: 23))
result.addAttributes([.font: NSFont.preferredFont(forTextStyle: .title2)], range: NSRange(location: 43, length: 13))
result.addAttributes([.font: NSFont.preferredFont(forTextStyle: .body)], range: NSRange(location: 57, length: 16))
result.addAttributes([.font: NSFont.preferredFont(forTextStyle: .title2)], range: NSRange(location: 74, length: 16))
result.addAttributes([.font: NSFont.preferredFont(forTextStyle: .body)], range: NSRange(location: 91, length: 18))

return result
}
return result
}()

struct ContentView: View {
var body: some View {
AttributedText(makeAttributedString())
AttributedText(attributedString)
.background(Color.gray.opacity(0.5))
.accentColor(.purple)
}
Expand Down
49 changes: 24 additions & 25 deletions Playgrounds/AttributedText_tvOS.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
import AttributedText
import PlaygroundSupport
import SwiftUI

import AttributedText

func makeAttributedString() -> 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
"""
)
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))
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
}
return result
}()

struct ContentView: View {
var body: some View {
AttributedText(makeAttributedString())
AttributedText(attributedString)
.background(Color.gray.opacity(0.5))
.accentColor(.purple)
}
Expand All @@ -37,5 +36,5 @@ struct ContentView: View {
PlaygroundPage.current.setLiveView(
ContentView()
.frame(width: 800, height: 600)
.background(Color.yellow)
.background(Color(.darkGray))
)
56 changes: 55 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,57 @@
# AttributedText

A description of this package.
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
```swift
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](iOS_screenshot.png)

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…**
1. Enter `https://github.com/gonzalezreal/AttributedText` into the package repository URL text field
1. Link **AttributedText** to your application target
1 change: 0 additions & 1 deletion Sources/AttributedText/AttributedText_UIKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@
textView.isScrollEnabled = false
textView.backgroundColor = .clear
textView.textContainer.lineFragmentPadding = 0
textView.adjustsFontForContentSizeCategory = true
textView.delegate = self
}
}
Expand Down
Binary file added iOS_screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2adf1b8

Please sign in to comment.