Last active
January 18, 2023 13:25
-
-
Save Thomvis/98c8a1f9e252bfed56b63f917208ef5d to your computer and use it in GitHub Desktop.
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 { | |
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