Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Change Color Of Form Labels

How do I change the color of form labels in SwiftUI?

For example, Kundennummer should be gray, but 1033 should be white.

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have 3 different forms:

// "Kundennummer"
TextField("Kundennummer", text: $customerNumber)

// "Anrede"
Picker("Anrede", selection: $previewIndex) {
    ForEach(0 ..< previewOptions.count) {
        Text(self.previewOptions[$0])
    }
}

// Section "Kontakt"
Section(header: Text("Kontakt")) {
    TextField("Telefon", text: $phone)
}

>Solution :

Form elements have many different initialisers. You’re currently using the form that takes a string as the first argument and creates a Text view using that string. But there are other forms that let you use a view block to create your own form label – and that label can be any SwiftUI view, including a Text with its colour changed from the default.

For example, in place of

TextField("Kundennummer", text: $customerNumber)

you could use

TextField(text: $customerNumber) {
  Text("Kundennummer")
    .foregroundColor(.secondary)
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading