skip to Main Content

I found an issue with TextField in SwiftUI. I used a TextField in my app and set a placeholder that is a link like ‘www.abcdefg.com’. However, the placeholder color is blue instead of gray. I don’t know how to change the placeholder text color or disable link detection for the TextField. Does anyone know how to fix this? Thanks!

Here is a simple code for question
enter image description here

2

Answers


  1. This is because of markdown. You can try this approach using prompt from iOS 15, now you’re able to change the color, font, etc… to whatever you want.

    TextField(
        "",
        text: $text,
        prompt: Text(verbatim: "www.abc.com") //<- here
            .foregroundStyle(Color.red)       //<- here
            .font(.headline)                  //<- here
    )
    
    Login or Signup to reply.
  2. The TextField initialiser might be using this Text initialiser under the hood to create the label. This detects markdown and renders it as such. You should use TextField.init(text:label:) that allows you to pass in any View as a label instead. Then you can use Text.init(verbatim:) to force it to not treat the text as markdown.

    TextField(text: $text) {
        Text(verbatim: "www.example.com")
    }
    

    Alternatively, you can use backslashes to escape the dots in the URL, so SwiftUI no longer detects it as a URL.

    TextField("www\.example\.com", text: $text)
    

    Other solutions here may also work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search