diff --git a/README.md b/README.md index c9a371f..762146d 100644 --- a/README.md +++ b/README.md @@ -183,6 +183,17 @@ In UIKit, set `isSelectable` on `TranscriptTextView`. `TranscriptStyle` sets the body size, line, block and list spacing, indents, padding and the maximum image height. +It also sets the colors. Each one is a `TranscriptColor`, with a value for light and one for dark appearance, so the +text can be drawn off the main thread. A color that is nil uses the system color shown here: + +```swift +var style = TranscriptStyle() +style.textColor = TranscriptColor(UIColor.label) // the text +style.linkColor = TranscriptColor(UIColor.systemIndigo) // links and their underlines +style.codeBackgroundColor = TranscriptColor(UIColor.secondarySystemFill) // code blocks, table headers +style.accentColor = TranscriptColor(UIColor.tertiaryLabel) // quote bars, rules, table lines +``` + ### Reading the selection `onSelectionChange` tells the app when the selection changes. The range is in the document's flat UTF-16 offset diff --git a/Sources/TranscriptKit/Layout/TranscriptStyle.swift b/Sources/TranscriptKit/Layout/TranscriptStyle.swift index 8f0fda3..0aa7378 100644 --- a/Sources/TranscriptKit/Layout/TranscriptStyle.swift +++ b/Sources/TranscriptKit/Layout/TranscriptStyle.swift @@ -19,6 +19,17 @@ public struct TranscriptStyle: Equatable, Sendable { /// The tallest an image is shown; wider ones are scaled to fit the width. public var maximumImageHeight: CGFloat = 360 + /// The color of the text. Nil uses the system's label color. + public var textColor: TranscriptColor? + /// The color of links and their underlines. A custom tag's color wins over it. + public var linkColor = TranscriptColor(red: 0, green: 0.478, blue: 1) + /// Behind code blocks, table headers and image placeholders. Nil uses the + /// system's secondary fill color. + public var codeBackgroundColor: TranscriptColor? + /// Quote bars, thematic breaks and table lines. Nil uses the system's + /// tertiary label color. + public var accentColor: TranscriptColor? + public init() {} struct Insets { @@ -111,10 +122,8 @@ public struct TranscriptStyle: Equatable, Sendable { } static let fontKey = NSAttributedString.Key(kCTFontAttributeName as String) - private static let colorKey = NSAttributedString.Key(kCTForegroundColorAttributeName as String) private static let colorFromContextKey = NSAttributedString.Key(kCTForegroundColorFromContextAttributeName as String) private static let runDelegateKey = NSAttributedString.Key(kCTRunDelegateAttributeName as String) - private static let linkColor = CGColor(red: 0, green: 0.478, blue: 1, alpha: 1) /// Core Text has no strikethrough, and its underline does not survive /// run-by-run drawing, so both are marked and drawn by `DocumentLayout`. static let strikethroughKey = NSAttributedString.Key("TranscriptKit.strikethrough") @@ -199,8 +208,8 @@ public struct TranscriptStyle: Equatable, Sendable { // Drawn in the context's fill color, which `DocumentLayout` sets to it. result.addAttribute(Self.foregroundKey, value: foreground, range: range) } else if isLink { - result.removeAttribute(Self.colorFromContextKey, range: range) - result.addAttribute(Self.colorKey, value: Self.linkColor, range: range) + // Resolved for light or dark when drawn, like a tag's color. + result.addAttribute(Self.foregroundKey, value: linkColor, range: range) } if let background { result.addAttribute(Self.backgroundKey, value: background, range: range) } if let image, range.length == 1 { diff --git a/Sources/TranscriptKit/iOS/TranscriptTextView.swift b/Sources/TranscriptKit/iOS/TranscriptTextView.swift index 2b9b5a7..540b7e5 100644 --- a/Sources/TranscriptKit/iOS/TranscriptTextView.swift +++ b/Sources/TranscriptKit/iOS/TranscriptTextView.swift @@ -94,6 +94,7 @@ public final class TranscriptTextView: UIView, UITextInput, UIGestureRecognizerD didSet { guard style != oldValue else { return } relayoutNow() + updateTailStyle() } } @@ -444,11 +445,13 @@ public final class TranscriptTextView: UIView, UITextInput, UIGestureRecognizerD } private func resolvedColors() -> TailRenderer.Colors { - TailRenderer.Colors( - text: UIColor.label.resolvedColor(with: traitCollection).cgColor, - codeBackground: UIColor.secondarySystemFill.resolvedColor(with: traitCollection).cgColor, - accent: UIColor.tertiaryLabel.resolvedColor(with: traitCollection).cgColor, - dark: traitCollection.userInterfaceStyle == .dark + let dark = traitCollection.userInterfaceStyle == .dark + return TailRenderer.Colors( + text: style.textColor?.cgColor(dark: dark) ?? UIColor.label.resolvedColor(with: traitCollection).cgColor, + codeBackground: style.codeBackgroundColor?.cgColor(dark: dark) + ?? UIColor.secondarySystemFill.resolvedColor(with: traitCollection).cgColor, + accent: style.accentColor?.cgColor(dark: dark) ?? UIColor.tertiaryLabel.resolvedColor(with: traitCollection).cgColor, + dark: dark ) } diff --git a/Tests/TranscriptKitTests/StyleColorTests.swift b/Tests/TranscriptKitTests/StyleColorTests.swift new file mode 100644 index 0000000..71d7cd4 --- /dev/null +++ b/Tests/TranscriptKitTests/StyleColorTests.swift @@ -0,0 +1,27 @@ +import Foundation +import Testing +@testable import TranscriptKit + +@Test func linksTakeTheStyleLinkColor() { + var style = TranscriptStyle() + style.linkColor = TranscriptColor(light: TranscriptColor(red: 1, green: 0, blue: 0), + dark: TranscriptColor(red: 0, green: 1, blue: 0)) + let block = MarkdownStreamParser.document(from: "See [the docs](https://example.com) now.").blocks[0] + let text = style.attributedString(for: block) + let link = (text.string as NSString).range(of: "the docs") + let color = text.attribute(TranscriptStyle.foregroundKey, at: link.location, effectiveRange: nil) as? TranscriptColor + #expect(color == style.linkColor) + #expect(text.attribute(TranscriptStyle.foregroundKey, at: 0, effectiveRange: nil) == nil) +} + +@Test func aTagColorWinsOverTheLinkColor() { + let tag = TranscriptTag("mark", TranscriptDecoration(foreground: TranscriptColor(red: 0, green: 0, blue: 1))) + var parser = MarkdownStreamParser(tags: [tag]) + parser.append("[tagged link](https://example.com)") + let block = parser.document.blocks[0] + var style = TranscriptStyle() + style.linkColor = TranscriptColor(red: 1, green: 0, blue: 0) + let text = style.attributedString(for: block) + let color = text.attribute(TranscriptStyle.foregroundKey, at: 0, effectiveRange: nil) as? TranscriptColor + #expect(color == TranscriptColor(red: 0, green: 0, blue: 1)) +}