I am new to swift but not new to programming but I have little experience with UI work.
I’m creating my first simple app that converts temperatures from celsius to Fahrenheit and vice versa.
When the user taps on the text field to enter their temperature, they have to manually delete the default value 0. However, it would be better if, when the user taps the textfield either a) the default text is highlighted so whatever they type automatically overwrites the default value or b) the default value disappears completely to show the word “temperature” until the user types in their value.
Is there a simple way to do this?
This is the code I have and a picture of what is currently happening.
”’
import SwiftUI
struct ContentView: View {
@State private var temp: Int = 0
@State private var tempUnit: String = ""
@State private var tempConvertUnit: String = ""
var convertedTemp: Int {
switch(tempUnit){
case "F":
if tempConvertUnit == "C"{
return Int((temp - 32) * 5 / 9)
}
else {
return temp
}
case "C":
if tempConvertUnit == "F"{
return Int(temp * 9/5 + 32)
}
else {
return temp
}
default:
return 0
}
}
let tempUnits = ["F", "C"]
@FocusState private var amountIsFocused: Bool
var body: some View {
NavigationStack{
Form{
Section("Temperature"){
TextField("Temperatue", value: $temp, format: .number)
.keyboardType(.decimalPad)
.focused($amountIsFocused)
Picker("Unit", selection: $tempUnit){
ForEach(tempUnits, id: .self){
unit in Text(unit)
}
}.pickerStyle(.segmented)
}
Section("Converted Temperature"){
Text(convertedTemp, format: .number)
Picker("Unit", selection: $tempConvertUnit){
ForEach(tempUnits, id: .self){
unit in Text(unit)
}
}.pickerStyle(.segmented)
}
}.navigationTitle("Converter")
.toolbar {
if amountIsFocused{
Button("Done"){
amountIsFocused = false
}
}
}
}
}
”’