I m making a TimerView in swiftui getting a start time and stop time on button pressed.
I am using two VStack inside a HStack to ensure that the text are aligned properly, but I am unable to account for the timestamp text size that are sometimes too large and goes out of alignment (timestamp is lower than the label!) (click start) or that the two different timestamps have different sizing (click stop)
How should I ensure that the text is uniformly sized while fitting to the window (the text was too long and went out of bounds)?
import SwiftUI
struct TimerView: View {
@State var fromTime = ""
@State var toTime = ""
var body: some View {
VStack (alignment: .leading){
HStack{
VStack (alignment: .leading){
Text("From Time: ").padding(.bottom, 3.0)
Text("To Time: ")
}
VStack (alignment: .leading) {
Text(fromTime).minimumScaleFactor(0.2).padding(.bottom, 3.0)
Text(toTime).minimumScaleFactor(0.2)
}
//Text(fromTime).minimumScaleFactor(0.2)
}.padding(.bottom, 60.0)
// HStack{
// Text("To Time: ")
// Text(toTime).minimumScaleFactor(0.2)
// }.padding(.bottom, 60.0)
HStack{
Button(action: {fromTime = getDate()}) {
Text("Start")
}.padding(.top, 10.0)
.padding(.bottom, 10.0)
.padding(.trailing,40.0)
.padding(.leading, 40.0)
.overlay(
RoundedRectangle(cornerRadius: 10.0).stroke(lineWidth: 2.0)
)
Button(action: {toTime = getDate()}) {
Text("Stop")
}.padding(.top, 10.0)
.padding(.bottom, 10.0)
.padding(.trailing, 40.0)
.padding(.leading, 40.0)
.overlay(
RoundedRectangle(cornerRadius: 10.0)
.stroke(lineWidth: 2.0)
)
}
}
}
}
struct TimerView_Previews: PreviewProvider {
static var previews: some View {
TimerView()
}
}
extension TimerView {
func getDate()->String{
let time = Date()
let timeFormatter = DateFormatter()
timeFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let stringDate = timeFormatter.string(from: time)
return stringDate
}
}