This is a simple post which describes how I went about implementing a feature to force a UI Label to display text in upper case. It is intended to be a suggestion for a future feature of NGUI and a guide for other who wish to implement this feature themselves. Hopefully someone can find this useful.
Written for C#.
First of all, there is an extremely handy inbuilt function with the string library called ToUpper() which does what you think, converts a string to upper case, this is exactly what we need.
First of all we need to add a few variables to the UILabel class so we can mark labels as 'Force caps/upper case'
// In UILabel.cpp
[HideInInspector][SerializeField] string mText_NonCaps = "";
[HideInInspector][SerializeField] bool mForceCaps = false;
mForceCaps is pretty simple, just a check box so we can mark as UI label as upper case only. mText_NonCaps is used to preserve the old string so that we can dynamically switch between the upper case string and the original string that has been entered.
For example. In a text box you might enter the string "Awesome String". Ticking the Force Caps button will convert that to "AWESOME STRING". Because we have stored the original string ("Awesome String") we can simply untick the Force Caps and the text will revert to the original, non upper case version (If you made a designer re-enter all the text if they pressed it by accident they would NOT be happy).
Next we need to expose the toggle to the inspector.
// In UILabelInspector.cpp
override protected bool OnDrawProperties ()
{
// Existing code here
bool forceCaps = EditorGUILayout.Toggle("Force Caps", mLabel.ForceCaps);
if (forceCaps != mLabel.ForceCaps) { RegisterUndo(); mLabel.ForceCaps = forceCaps; }
return true;
}
This just places a tick box in the UILabel inspector. If you've been following this your code will
not compile as forceCaps is a private variable. I left the accessor to last as it contains most of the logic we care about. When changing the tick box we need to store the original string and then make the current upper case OR restore the original string.
// In UILabel.cpp
public bool ForceCaps
{
get { return mForceCaps; }
set
{
// If we are going to force caps then save the text so we can restore it
if (value)
{
mText_NonCaps = mText;
mText = mText.ToUpper();
}
else // Set the text back to the non caps value
{
mText = mText_NonCaps;
mText_NonCaps = "";
}
mForceCaps = value;
}
}
Luckily I added comments so this is very self explanitory. If we are ticking the box (Wanting to force upper case) then we save the original string to the mText_NonCaps variable then make the text upper case with the handy C# function I meantioned at the start. If we are unticking the box then we want to restore the original value and I clear the non caps value just to be nice.
The final step is to modify the text entry so it will respect the setting when entering text.
// In UILabel.cpp. This is a modification of an existing function
public string text
{
get
{
return mText;
}
set
{
if (value != null && mText != value)
{
if (mForceCaps)
{
mText_NonCaps = value;
mText = ((string)value).ToUpper();
}
else
{
mText = value;
}
hasChanged = true;
}
}
}
This simply saves the original value and converts the new one to upper case if that option is ticked, if not it will just enter it as normal.
There is an oddity with Unity (or my code) where if you tick Force Caps and start typing it will display as normal text in the inspector but in the game/scene it will be upper case. When you click out of the text edit box then the text in the inspector will update to be upper case.
I hope this has been helpful to at least one person. If you have any questions about this just post!