Is it possible to add setInputRestrictions to Label ?
In the Label class add two private members:
int maxTextLength;
String allowedCharacters;
and add the public method:
/** Sets limits on the characters that can be entered.
@param maxTextLength if this is > 0, it sets a maximum length limit; if 0, no
limit is set
@param allowedCharacters if this is non-empty, then only characters that occur in
this string are allowed to be entered into the editor.
*/
void setInputRestrictions (int maxTextLength,
const String& allowedCharacters = String::empty);
In the Label constructor add:
Label::Label (const String& name,
const String& labelText)
: Component (name),
textValue (labelText),
lastTextValue (labelText),
font (15.0f),
justification (Justification::centredLeft),
horizontalBorderSize (5),
verticalBorderSize (1),
minimumHorizontalScale (0.7f),
editSingleClick (false),
editDoubleClick (false),
lossOfFocusDiscardsChanges (false),
maxTextLength (-1)
{
setColour (TextEditor::textColourId, Colours::black);
setColour (TextEditor::backgroundColourId, Colours::transparentBlack);
setColour (TextEditor::outlineColourId, Colours::transparentBlack);
textValue.addListener (this);
}
to initialize maxTextLength to -1.
Add the method:
void Label::setInputRestrictions (const int maxLen,
const String& chars)
{
maxTextLength = jmax (0, maxLen);
allowedCharacters = chars;
if (editor)
editor->setInputRestrictions(maxTextLength, allowedCharacters);
}
and finally modify Label::showEditor() :
void Label::showEditor()
{
if (editor == nullptr)
{
addAndMakeVisible (editor = createEditorComponent());
editor->setText (getText(), false);
editor->addListener (this);
editor->grabKeyboardFocus();
editor->setHighlightedRegion (Range<int> (0, textValue.toString().length()));
if (maxTextLength != -1)
editor->setInputRestrictions(maxTextLength, allowedCharacters);
resized();
repaint();
editorShown (editor);
enterModalState (false);
editor->grabKeyboardFocus();
}
}
Thanks,
Rail