Skip to content

Commit

Permalink
✨ Add support for multiple text styles in a single primitive
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaatttt committed Feb 9, 2023
1 parent 55c75a1 commit 84c0c31
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ extension ExperienceComponent {
let id: UUID
let text: String

let spans: [TextSpan]?

let style: Style?

var textDescription: String? { text }
Expand Down Expand Up @@ -285,6 +287,13 @@ extension ExperienceComponent {
let width: Double
let height: Double
}

struct TextSpan: Decodable {
let text: String

/// Note: not all style properties can be applied to a `TextSpan`.
let style: Style
}
}

extension ExperienceComponent.Style {
Expand Down
36 changes: 35 additions & 1 deletion Sources/AppcuesKit/Presentation/UI/Components/AppcuesText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,44 @@ internal struct AppcuesText: View {
var body: some View {
let style = AppcuesStyle(from: model.style)

Text(model.text)
Text(textModel: model)
.applyTextStyle(style, model: model)
.setupActions(on: viewModel, for: model)
.applyAllAppcues(style)
.fixedSize(horizontal: false, vertical: true)
}
}

@available(iOS 13.0, *)
extension Text {
init(textModel: ExperienceComponent.TextModel) {
if let spans = textModel.spans {
self.init("")

// Note: a ViewBuilder approach here doesn't work because the requirement that we operate strictly on `Text`
// and not `some View` for concatenation to work. Therefore we work with the struct directly.
spans.forEach { span in
let style = AppcuesStyle(from: span.style)
var text = Text(span.text)

if let font = style.font {
text = text.font(font)
}

if let foregroundColor = style.foregroundColor {
text = text.foregroundColor(foregroundColor)
}

if let kerning = style.letterSpacing {
text = text.kerning(kerning)
}

// A shorthand operator with `Text` doesn't compile
// swiftlint:disable:next shorthand_operator
self = self + text
}
} else {
self.init(textModel.text)
}
}
}

0 comments on commit 84c0c31

Please sign in to comment.