Hstack with Sf Symbols Image Not Aligned Centered

HStack with SF Symbols Image not aligned centered

This is what it's going on.

Sample Image

The Image views are not resizing.

It looks like they're not aware of their intrinsic content size, or maybe it reports the wrong value.

To fix it:

struct ContentView : View {
var body: some View {
HStack(alignment: .center, spacing: 10.0) {
Image(systemName: "cloud.sun")
.resizable()
.aspectRatio(contentMode: .fit)
.background(Color.red)
Image(systemName: "cloud")
.resizable()
.aspectRatio(contentMode: .fit)
.background(Color.yellow)
Image(systemName: "cloud.bolt")
.resizable()
.aspectRatio(contentMode: .fit)
.background(Color.pink)
Text("Text").background(Color.green)
}
.frame(width: 250, height: 50)
.background(Color.gray)
.font(.title)

}
}

...make the Images resizable, and also make sure the aspect ratio is set to .fit, or they will stretch.

Set also frame on the HStack or it will expand to fill the whole screen.

@MartinR suggested an even better solution - creating the images via UIImage - see his comment below.

struct ContentView : View {

var body: some View {
HStack {
Image(uiImage: UIImage(systemName: "cloud.sun")!)
.background(Color.red)
Image(uiImage: UIImage(systemName: "cloud")!)
.background(Color.yellow)
Image(uiImage: UIImage(systemName: "cloud.bolt")!)
.background(Color.pink)
Text("Text").background(Color.green)
}
.background(Color.gray)
.font(.title)

}

}

Output:

Sample Image

How do I center these images in a straight line inside my HStack in SwiftUI

Wrapping everything in a geometryreader and assigning relative width to your textviews should do the trick:

var body: some View {
//decide which image to show
GeometryReader{ readerProxy in <- add this
HStack(alignment: .center){
Text(dailyData.time)
.fontWeight(.semibold)
.foregroundColor(Color(UIColor.white))
.font(.system(size: 16))
.frame(width: readerProxy.size.width / 3, alignment: .left) <- add this
Spacer()
Image(dailyData.icon)
.resizable()
.scaledToFit()
.frame(width: 25, height: 25, alignment: .center)
.multilineTextAlignment(.leading)
Spacer()
HStack(spacing: 10){
Text(dailyData.lowtemp + "°")
.fontWeight(.regular)
.foregroundColor(Color(UIColor.white))
.font(.system(size: 16))
Text(dailyData.hightemp + "°")
.fontWeight(.regular)
.foregroundColor(Color(UIColor.white))
.font(.system(size: 16))
}
.frame(width: readerProxy.size.width / 3, alignment: .right) <- add this
}
}
}

SwiftUI align SF Symbol as text with a text

I don’t know if you can force the image to automatically be centred in all cases, but you can add a .baselineOffset() modifier to the image-in-text to shift the image upwards by a fixed amount. For example:


Text(Image(systemName: "circle.hexagongrid.circle"))
.font(.caption)
.foregroundColor(.pink)
.baselineOffset(3.0)
+
Text("A long text as example")
.font(.title2).bold()
.foregroundColor(.pink)

Once you have hit upon the right offset amount for the default text size, you might be able to accommodate accessibility variations by making the offset size relative to a base size, e.g.

Struct MyView: View {
@ScaledMetric(relativeTo: .title2) var baselineOffset: CGFloat = 3.0

// and in your body…
.baselineOffset(baselineOffset)

How do i align an image with text in an HStack?

You just need to remove padding from Text("500") and set to HStack

Please try with below code and let me know it's works for you

VStack(alignment: .center, spacing: 0) {

Image("ProductLogo")
.resizable()
.aspectRatio(contentMode: .fit)
.clipped()
.padding()
.layoutPriority(1)


Text("Text Here")
.font(.title2)
.fontWeight(.bold)
.foregroundColor(.black)
.minimumScaleFactor(0.5)
.multilineTextAlignment(.center)
.padding(.leading, 5)
.padding(.trailing, 5)
.padding(.bottom, 5)


Text("Subtitle")
.font(.headline)
.foregroundColor(.gray)
.lineLimit(2)
.padding(1)

//HERE IS WHAT NEEDS TO BE ALIGNED
HStack {

Image("CoinImage")
.resizable()
.frame(width: 35, height: 35, alignment: .center)

Text("500")
.font(.system(size: 20))

}.padding(.top, 10)
//END OF HSTACK
}

How to make SF Symbols in SwiftUI have the same width

A workaround for the issue. However, the problem is that it does not work well with dynamic font sizing.

HStack {
Image(systemName: "square.and.arrow.up")
.font(Font.system(size: 18, weight: .medium, design: .monospaced))
.foregroundColor(Color("secondary"))
}
.frame(minWidth: 30)

Sample Image

Aligning texts and images in SwiftUI

Images can have baselines, and although many times they are equal to zero, there are some other cases (like "Cloudy"), when they're not. If you set the baseline to zero, then it will center with the text:

struct ContentView: View {

var body: some View {
HStack(alignment: .center) {
Text("Cloudy").background(Color.yellow)
Image(uiImage: UIImage(systemName: "cloud.heavyrain")!.withBaselineOffset(fromBottom: 0))
.background(Color.blue)
}
.font(.largeTitle)
.border(Color.red)
}
}


Related Topics



Leave a reply



Submit