Yes, there is an approach to align custom fonts to the center in SwiftUI. You can use the .baselineOffset modifier to adjust the vertical alignment of the custom font.
Here's an updated version of your code that centers the custom font:
`struct ContentView: View {
var body: some View {
VStack(spacing: 20){
Text("SEPTEMBER")
.font(.largeTitle)
.border(.blue)
Text("SEPTEMBER")
.font(Font.custom("Arial Hebrew", size: 20))
.baselineOffset(-0.25 * UIFont.preferredFont(forTextStyle: .body).lineHeight)
.border(.red)
}
}
}`
In this code, we're using the .baselineOffset modifier to move the baseline of the custom font down by a fraction of its line height. The specific value we're using (-0.25) is just a suggestion; you may need to adjust it to get the exact center alignment you want.
Note that we're also using UIFont.preferredFont(forTextStyle: .body).lineHeight to get the line height of the system font at the .body text style. This ensures that the offset will be proportional to the font size, and will work correctly on devices with different screen densities.
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: