- Home /
Single-line GUI.TextField with custom GUIStyle
I meet some troubles when I try to make input field with custom style on iOS. I need a keyboard without any additional fields, only buttons. When I make
TouchScreenKeyboard.hideInput = true;
it works perfectly, but when I try to use my GUIStyle for customize font and background of input field, it becomes a multiline, and hideInput becomes useless.
There are example of my code:
GUIStyle myCustomStyle = new GUIStyle();
myCustomStyle.font = Resources.Load("myfont") as Font;
myCustomStyle.normal.textColor = Color.blue;
GUI.TextField(new Rect(0,0,200,50), "hello world!", myCustomStyle);
As a result, I get a multi-line input field (but if I call GUI.TextField without specified GUIStyle, it is single-line), and hideInput does not have any effect with this field.
How can I make single-line GUI.TextField with custom GUIStyle?
Answer by Sundar · Aug 23, 2012 at 04:17 PM
Check font size, is it same as default font, if not make it same. Also, you can set your GUIStyle wordWrap to false and clipping set to clip like
myCustomStyle.wordWrap = false;
myCustomStyle.clipping = TextClipping.clip;
Unfortunately, it didnt works. When I pass any style to TextField, it becomes multi-line. Even if I do
GUIStyle myStyle = new GUIStyle(GUI.skin.textField);
someString = GUI.TextField(someRect, someString, myStyle);
it results with multi-line textfield, but
someString = GUI.TextField(someRect, someString);
give me single-line textfield.
Can you check Debug.Log(myCustomStyle.imagePosition );
if it is not "TextOnly", then assign myCustomStyle.imagePosition = TextOnly;
I check imagePosition and it is TextOnly. But when I digging around, I found out what I was stupid. Problem appears not when I specify style, but when i specify max length and style. I.e. code like
someString = GUI.TextField(someRect, someString, 40);
or
someString = GUI.TextField(someRect, someString, myStyle);
works perfectly, like simple
someString = GUI.TextField(someRect, someString);
but I get multi-line text field when I make
someString = GUI.TextField(someRect, someString, 40, myStyle);
Sorry for misleading you in my previous posts.
Just curious: am I the only one getting a single-line TextArea?
Answer by Neodim · Aug 24, 2012 at 01:37 PM
I check imagePosition and it is TextOnly. But when I digging around, I found out what I was stupid. Problem appears not when I specify style, but when i specify max length and style. I.e. code like
someString = GUI.TextField(someRect, someString, 40);
or
someString = GUI.TextField(someRect, someString, myStyle);
works perfectly, like simple
someString = GUI.TextField(someRect, someString);
but I get multi-line text field when I make
someString = GUI.TextField(someRect, someString, 40, myStyle);
Sorry for misleading you in my previous posts.
Answer by naii · Sep 15, 2014 at 12:19 PM
Found this old topic while searching for something else, in case someone would be interested in this though... maybe it would be worth trying GUI.skin.textField
GUIStyle myStyle = new GUIStyle(GUI.skin.textField);
someString = GUI.TextField(someRect, someString, 40, myStyle);