Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI - How do I change the text color of a Textfield?

I'm trying to change the textcolor of Textfield (the text that user write) in SwiftUI. I'm trying with foregroundColor but it changes the placeholder's text color.

How do you do this in SwiftUI?

 TextField($variable, placeholder: Text("a text"))
            .foregroundColor(.green)
like image 912
Yhondri Avatar asked Jun 22 '19 12:06

Yhondri


People also ask

How do I change the color of text TextField UI?

To change the text field font color in React Material UI, we call the makeStyles function with an object with the styles we want to apply. to call makeStyles with an object that has the input property that's set to an object with the color property set to 'blue' . Next, we call useStyles to return the classes object.

How do I change the TextField color in Swift?

Select the text field -> click "identity inspector" icon -> click on Plus button "user Defined runtime attributes" -> add this text "_placeholderLabel. textColor" in "key path" field -> choose the "Type" "color" and set the value color.

How do I customize text in SwiftUI?

Hold the command key and click the text to bring up a pop-over menu. Choose Show SwiftUI Inspector and then you can edit the text/font properties.

How do I change text color in Xcode?

Xcode Getting started with Xcode Changing The Color SchemeWith the preference pane open you can click on the 'Fonts and Colors' tab. From here you can change the source AND console background and font colors.


3 Answers

.foregroundColor actually does change the text color of TextField but only when it has a default value, for example this will work fine:

TextField(.constant("Hello World"), placeholder: Text("Type here..."))
  .foregroundColor(.green)

But once you remove the whole text, text field loses not only its color but the rest of its modifiers such as its alignment as well. This is likely a bug in the current version so I'll file a bug report.


Update: This issue was fixed with Xcode 11 beta 3.

like image 160
M Reza Avatar answered Oct 14 '22 06:10

M Reza


Use background() to set background color and foreground() to set input text color

struct PlaygroundTestView: View {

@State var searchText = ""

var body: some View {
    
    TextField("Search", text: $searchText)
    .font(Font.system(size: 14))
    .padding()
    .background(RoundedRectangle(cornerRadius: 50).fill(Color.white))
    
    .foregroundColor(.black)
    .padding()
    .offset(x:8, y: 10)
}

}

like image 22
Steve Yuan Avatar answered Oct 14 '22 08:10

Steve Yuan


Text color should be changed for using the Property of text field .foreground color Updated for Xcode Version 11.1 (11A1027)

SwiftUI

TextField("Email", text: $email)
                .textContentType(.emailAddress)
                .padding(.init(top: 0, leading: 15, bottom:0 , trailing: 15))
                .foregroundColor(.blue)
                .textFieldStyle(RoundedBorderTextFieldStyle.init())
like image 39
Srinivasan_iOS Avatar answered Oct 14 '22 06:10

Srinivasan_iOS