Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 13 additions & 4 deletions Sources/TranscriptKit/Layout/TranscriptStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 8 additions & 5 deletions Sources/TranscriptKit/iOS/TranscriptTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public final class TranscriptTextView: UIView, UITextInput, UIGestureRecognizerD
didSet {
guard style != oldValue else { return }
relayoutNow()
updateTailStyle()
}
}

Expand Down Expand Up @@ -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
)
}

Expand Down
27 changes: 27 additions & 0 deletions Tests/TranscriptKitTests/StyleColorTests.swift
Original file line number Diff line number Diff line change
@@ -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("<mark>[tagged link](https://example.com)</mark>")
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))
}