Created
January 10, 2026 18:59
-
-
Save mike011/a89ca2844f01ef93d8f3a37479fb6def to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // GlowRendererView.swift | |
| // UnwrapLive26 | |
| // | |
| // Created by Michael Charland on 2026-01-10. | |
| // | |
| import SwiftUI | |
| struct GlowAttribute: TextAttribute {} | |
| struct GlowRenderer: TextRenderer { | |
| var color: Color | |
| func draw(layout: Text.Layout, in context: inout GraphicsContext) { | |
| for line in layout { | |
| for run in line { | |
| if run[GlowAttribute.self] != nil { | |
| for radius in stride(from: 8.0, through: 1.0, by: -3.0) { | |
| var glowContext = context | |
| glowContext.addFilter(.blur(radius: radius)) | |
| glowContext.fill( | |
| Rectangle().path( | |
| in: run.typographicBounds.rect.insetBy( | |
| dx: -radius, | |
| dy: -radius / 2 | |
| ) | |
| ), | |
| with: .color(color) | |
| ) | |
| } | |
| } | |
| context.draw(run) | |
| } | |
| } | |
| } | |
| } | |
| struct GlowRendererView: View { | |
| @State private var startTime = Date.now | |
| var body: some View { | |
| TimelineView(.animation) { timeline in | |
| let elapsed = timeline.date.timeIntervalSince(startTime) | |
| let glowIntensity = abs(sin(elapsed)) * 0.2 + 0.1 | |
| let demo = Text("demo") | |
| .customAttribute(GlowAttribute()) | |
| Text("This is a \(demo) of the custom highlight renderer.") | |
| .font(.system(size: 72)) | |
| .lineSpacing(40) | |
| .textRenderer(GlowRenderer(color: .yellow.opacity(glowIntensity))) | |
| } | |
| } | |
| } | |
| #Preview { | |
| GlowRendererView() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment