- Home /
Change the color of a label font that's using an EditorStyle.textField style?
Hello,
I have a custom object field which essentially calls GUILayout(field, EditorStyles.textField)
to get some resemblance to an object field. However, I'm not being able to change the color of text that's been displayed.
public static void DraggableLabeledObjectField(string label, string field, Object value, bool ping, float labelWidth, GUIStyle style = null)
{
CustomLabelWidthBlock(labelWidth, () =>
{
EditorGUILayout.PrefixLabel(label);
GUILayout.Label(field, style ?? EditorStyles.textField);
var labelRect = GUILayoutUtility.GetLastRect();
GUIHelper.RegisterFieldForDrag(labelRect, value);
if (ping) {
EditorGUIUtility.AddCursorRect(labelRect, MouseCursor.Zoom);
if (GUI.Button(labelRect, "", GUIStyle.none))
Utils.PingObject(value);
}
});
}
I tried GUI.contentColor
it didn't do anything for some reason. I figured, I'm not passing in a GUIContent
to my GUILayout.Label
call so I changed the first parameter from field
to new GUIContent(field)
, it didn't budge.
You'll notice that I'm using a null-coalescing operator, that's cause I tried to pass in GUIStyles
:
StateFieldLabel = new GUIStyle(EditorStyles.textField)
{
normal = new GUIStyleState()
{
textColor = Color.green
}
};
That did change the color, but I didn't get a textField
, all I got is just a normal label.
Here's what the fields look like:
Any ideas?
Thanks!
Blockquote
Answer by MakeCodeNow · Feb 19, 2014 at 07:42 AM
Just make a new GUIStyle and pass in your base Editor style into the constructer. Then, set the .normal.textColor of your new GUIStyle to whatever color you want. Finally, pass that GUIStyle into whatever GUIfunctions you want to have text drawn in that color.
Sorry but, have you not read my question? I mentioned that I did do that but I only got the color to change without getting the textField style (i.e. I got just a colored label, not a textField with colored content).
$$anonymous$$akeCodeNow is correct, here's what he's saying in code:
GUIStyle s = new GUIStyle(EditorStyles.textField);
s.normal.textColor = Color.green;
GUILayout.Label("fda34",s);
Which, for me, shows a green text within an unfocusable textfield.
Right. It seems that I shouldn't have created a new 'normal'
Your answer
![](https://koobas.hobune.stream/wayback/20220613133911im_/https://answers.unity.com/themes/thub/images/avi.jpg)