I am trying to make an HStack, where the views inside it are responsive to the size of the HStack frame. I have gotten this part down, but the issue I have is that the the text in the HStack does not reach the same height as the image beside it. Both containers are the same height — however the text view does not actually reach the full height of its container (as seen by the background).
Does anyone know how to fix this?
HStack {
Image(systemName: "globe")
.resizable()
.aspectRatio(contentMode: .fit)
.background(Color.red)
Text("Globe")
.font(.system(size: 100))
.minimumScaleFactor(0.1)
.background(Color.blue)
}.frame(height: 100)
This is the output my current code gives. I am looking to make the text reach the full height of its blue background.
2
Answers
Here is a SwiftUI solution that is quite similar.
However, it uses hardcoded values.
Here is an alternate solution using some UIKit.
The solution does not remove vertical padding but reduces it significantly.
Output
Original
I believe that space is necessary for certain characters, else they might end up getting clipped.
Characters using umlaut take that space
Lowercase characters like g,p etc use that bottom space.
Characters like back slash etc too.
If it is a fixed text, there might be some way to find the current text height of the characters used and apply a negative padding like @Md. Ibrahim Hassan has suggested. But I believe that the default space provided be left as is.