Skip to content

Instantly share code, notes, and snippets.

@mattyoung
Created September 13, 2021 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattyoung/0957fdb3c33f251e5bec3385ec0f5f88 to your computer and use it in GitHub Desktop.
Save mattyoung/0957fdb3c33f251e5bec3385ec0f5f88 to your computer and use it in GitHub Desktop.
import SwiftUI
struct ContentView: View {
@State var value = -122.90
let range = -300.0...300
var body: some View {
VStack {
#warning("`alignment: .firstTextBaseline`, cause the Text inside Label is elided sometime")
HStack(alignment: .firstTextBaseline) {
Label("Temperature", systemImage: "thermometer")
Text("\(value, specifier: "%.2f")")
.font(.title)
}
// if these modifiers are commented out, no elide problem
.padding()
.background(Color.gray)
.cornerRadius(10.0)
.border(Color.green)
HStack(alignment: .lastTextBaseline) {
Label("Temperature", systemImage: "thermometer")
Text("\(value, specifier: "%.2f")")
.font(.title)
}
.padding()
.background(Color.gray)
.cornerRadius(10.0)
.border(Color.green)
HStack(alignment: .top) {
Label("Temperature", systemImage: "thermometer")
Text("\(value, specifier: "%.2f")")
.font(.title)
}
.padding()
.background(Color.gray)
.cornerRadius(10.0)
.border(Color.green)
HStack {
Label("Temperature", systemImage: "thermometer")
Text("\(value, specifier: "%.2f")")
.font(.title)
}
.padding()
.background(Color.gray)
.cornerRadius(10.0)
.border(Color.green)
#warning("Add .fixedSize() to Label fix the problem, but it's not ideal")
HStack {
Label("Temperature", systemImage: "thermometer")
.fixedSize()
Text("\(value, specifier: "%.2f")")
.font(.title)
}
.padding()
.background(Color.gray)
.cornerRadius(10.0)
.border(Color.green)
Spacer()
Slider(value: $value, in: range,
step: 0.1,
minimumValueLabel: Text("\(range.lowerBound, specifier: "%.2f")"),
maximumValueLabel: Text("\(range.upperBound, specifier: "%.2f")"),
label: { Text("Value") })
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment