Skip to content

Instantly share code, notes, and snippets.

@Thomvis
Last active January 18, 2023 13:25
Show Gist options
  • Save Thomvis/98c8a1f9e252bfed56b63f917208ef5d to your computer and use it in GitHub Desktop.
Save Thomvis/98c8a1f9e252bfed56b63f917208ef5d to your computer and use it in GitHub Desktop.
struct ContentView: View {
var body: some View {
VStack(alignment: .customAlignment) {
HStack {
Text("100 m").font(Font.body.bold())
Text("Usain Bolt").setCustomAlignmentToLeading()
}
HStack {
Text("5 km").font(Font.body.bold())
Text("Joshua Cheptegei").setCustomAlignmentToLeading()
}
HStack {
Text("10 km").font(Font.body.bold())
Text("Rhonex Kipruto").setCustomAlignmentToLeading()
}
HStack {
Text("Marathon").font(Font.body.bold())
Text("Eliud Kipchoge").setCustomAlignmentToLeading()
}
}
}
}
extension View {
func setCustomAlignmentToLeading() -> some View {
self.alignmentGuide(HorizontalAlignment.customAlignment, computeValue: { $0[.leading] })
}
}
extension HorizontalAlignment {
private enum CustomAlignment: AlignmentID {
static func defaultValue(in d: ViewDimensions) -> CGFloat {
return d[.leading]
}
}
static let customAlignment = HorizontalAlignment(CustomAlignment.self)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment