diff --git a/.spi.yml b/.spi.yml deleted file mode 100644 index 8d77fb6..0000000 --- a/.spi.yml +++ /dev/null @@ -1,5 +0,0 @@ -version: 1 -builder: - configs: - - platform: watchos - scheme: AttributedText_watchOS diff --git a/Playgrounds/AttributedText_iOS.playground/Contents.swift b/Playgrounds/AttributedText_iOS.playground/Contents.swift index 64ae6c6..7034b10 100644 --- a/Playgrounds/AttributedText_iOS.playground/Contents.swift +++ b/Playgrounds/AttributedText_iOS.playground/Contents.swift @@ -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) } diff --git a/Playgrounds/AttributedText_macOS.playground/Contents.swift b/Playgrounds/AttributedText_macOS.playground/Contents.swift index 39cc96b..55960dd 100644 --- a/Playgrounds/AttributedText_macOS.playground/Contents.swift +++ b/Playgrounds/AttributedText_macOS.playground/Contents.swift @@ -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) } diff --git a/Playgrounds/AttributedText_tvOS.playground/Contents.swift b/Playgrounds/AttributedText_tvOS.playground/Contents.swift index 7a6d3bd..3cc675f 100644 --- a/Playgrounds/AttributedText_tvOS.playground/Contents.swift +++ b/Playgrounds/AttributedText_tvOS.playground/Contents.swift @@ -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) } @@ -37,5 +36,5 @@ struct ContentView: View { PlaygroundPage.current.setLiveView( ContentView() .frame(width: 800, height: 600) - .background(Color.yellow) + .background(Color(.darkGray)) ) diff --git a/README.md b/README.md index 38731a7..1d4a4e5 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/Sources/AttributedText/AttributedText_UIKit.swift b/Sources/AttributedText/AttributedText_UIKit.swift index c9b1495..570a9a5 100644 --- a/Sources/AttributedText/AttributedText_UIKit.swift +++ b/Sources/AttributedText/AttributedText_UIKit.swift @@ -99,7 +99,6 @@ textView.isScrollEnabled = false textView.backgroundColor = .clear textView.textContainer.lineFragmentPadding = 0 - textView.adjustsFontForContentSizeCategory = true textView.delegate = self } } diff --git a/iOS_screenshot.png b/iOS_screenshot.png new file mode 100644 index 0000000..7fbad94 Binary files /dev/null and b/iOS_screenshot.png differ