iosarraysswifttextfield

Placeholder text color of a TextField is not getting changed if the text is a link in SwiftUI


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


Solution

  • 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.