This file contains 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
import SwiftUI | |
// View to split up a string into Text views, split by spaces. | |
struct ContentText: View { | |
private var splitText: [String] | |
let count: Int | |
init(_ text: String) { | |
self.splitText = text.split(separator: " ").map { "\($0) " } | |
if text.hasPrefix(" ") { |
This file contains 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
// Determine the alignment of every view in the ZStack | |
func zStackViews(_ geometry: GeometryProxy) -> some View { | |
// These are used to track the current horizontal and vertical position | |
// in the ZStack. As a new text or link is added, horizontal is decreased. | |
// When a new line is required, vertical is decreased & horizontal is reset to 0. | |
var horizontal: CGFloat = 0 | |
var vertical: CGFloat = 0 | |
// Determine the alignment for the view at the given index | |
func forEachView(_ index: Int) -> some View { |
This file contains 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
let numberOfViewsInContent: Int | |
let view: AnyView | |
// Determine the number of views in the Content at the given index | |
switch content[index] { | |
case .text(let text): | |
numberOfViewsInContent = text.count | |
view = AnyView(text) | |
case .link(let link): | |
numberOfViewsInContent = 1 |
This file contains 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
... | |
var body: some View { | |
VStack { | |
GeometryReader { geometry in | |
// Needs to be .topLeading so we can modify alignments on top and leading | |
ZStack(alignment: .topLeading) { | |
self.zStackViews(geometry) | |
} | |
.background(calculateHeight($height)) | |
} |
This file contains 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
var body: some View { | |
VStack { | |
GeometryReader { geometry in | |
// Needs to be .topLeading so we can modify alignments on top and leading | |
ZStack(alignment: .topLeading) { | |
self.zStackViews(geometry) | |
} | |
.background(calculateHeight($height)) | |
} | |
}.frame(height: height) |
This file contains 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
struct ContentView: View { | |
// The current height of the view | |
@State private var height: CGFloat = 0 | |
var body: some View { | |
VStack { | |
GeometryReader { geometry in | |
// Needs to be .topLeading so we can modify alignments on top and leading | |
ZStack(alignment: .topLeading) { | |
... |
This file contains 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
enum Content<T: View> { | |
case text(ContentText) | |
case link(Link<T>) | |
} | |
// A collection of either ContentText or Link views | |
// This text taken from https://developer.apple.com/documentation/swiftui/link | |
let content: [Content<Text>] = [ | |
.text(ContentText("As with other views, you can style links using standard view modifiers depending on the view type of the link’s label. For example, a ")), | |
.link(Link("Text", destination: URL(string: "https://developer.apple.com/documentation/swiftui/text")!)), |
This file contains 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
struct ContentText: View { | |
private var splitText: [String] | |
let count: Int | |
init(_ text: String) { | |
self.splitText = text.split(separator: " ").map { "\($0) " } | |
if text.hasPrefix(" ") { | |
self.splitText = [" "] + self.splitText | |
} |
This file contains 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
struct LinkAndText: View { | |
var body: some View { | |
HStack { | |
Text("Hello world, my name is ") | |
Link("Emma", destination: URL(string: "https://emma.sh")!) | |
Text(". Sometimes I write Swift code.") | |
} | |
} | |
} |
This file contains 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
struct LinkAndText: View { | |
var body: some View { | |
Text("Hello world, my name is ") | |
Link("Emma", destination: URL(string: "https://emma.sh")!) | |
Text(". Sometimes I write Swift code.") | |
} | |
} |