Laura Ramírez
Laura Ramírez

Reputation: 107

TextField swiftui xcode 13

I have been working with xcode 12 and swiftui. In my app I have textFiel with a localizable placeholder in Spanish and English, I switch to xcode 13 and it doesn't show me my localizable placeholder

enter image description here

this only happens in TextField, with SecureField it does not happen even with Text

this is my code

struct ContentView: View {
  @State var email:String = ""
    
    var body: some View {
        VStack () {
           TextField("login5", text: self.$email)
                    .autocapitalization(.none)
                    .padding()
                    .background(RoundedRectangle(cornerRadius: 50).stroke(Color("grayColor")))
        }.padding(.horizontal, 20)
    }
}
                 

Localizable.strings

"login5" = "Correo eléctronico";

Upvotes: 2

Views: 1250

Answers (3)

Zachary Brown
Zachary Brown

Reputation: 1

I've had the exact same problem, going from Xcode 12 to 13. All of a sudden some (not all) of my text fields no longer show localized string. I was able to fix the problem by forcing:

TextField(LocalizedString("usernameLabel"), text: $username)

Instead of

Textfield("usernameLabel", text: $username)

Upvotes: 0

with SecureField in ios15, you can use the prompt parameter to get your localized string:

SecureField("purpose", text: $password, prompt: Text("login6"))

or using the label:

 SecureField(text: $password) {
     Text("login6")
 }

EDIT1:

This is the test code I'm using to show a working localized TextField and SecureField.

import SwiftUI

@main
struct TestApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

struct ContentView: View {
    @State var email = ""
    @State var password = ""
    @State var isVisible = false
    
    var body: some View {
        VStack (spacing: 55) {
            Button(action: { isVisible.toggle() }) {
                Text("Toggle isVisible")
            }
            TextField("login5", text: $email).border(.black)
            if isVisible {
                TextField("login6", text: $password).border(.green)
            } else {
                SecureField("password", text: $password, prompt: Text("login6")).border(.red)
            }
        }.padding(.horizontal, 20)
    }
}

Test Localizable.strings file.

"login5" = "hola login5";
"login6" = "contraseña";

EDIT2: alternative approach of manually using LocalizedStringKey,

TextField(LocalizedStringKey("login5"), text: $email)

Upvotes: 3

Apix_D
Apix_D

Reputation: 1101

Your main Problem is, like workingdog already said, you need to use text: $variable. That means for you declare your variable as @State var password = "" and use it like this..

struct ContentView: View {
    @State var password = ""
    ...
    if self.visible{
        TextField("login6", text: $password)
        ....
    } else {
        SecureField("login6", text: $password)
        ....
    }
}

Btw. next time post your code as code not as picture. Its easier to help you :) Hope I understand your problem correctly and this will be your solution.

Upvotes: 0

Related Questions