TextEditor Justification

Why is the label still using the left alignment when editing?

void Label::showEditor()
{
    if (editor == nullptr)
    {
        editor->grabKeyboardFocus();

        // Add this to multiply the UX with 9000
        editor->setJustification(getJustificationType());

        if (editor == nullptr) // may be deleted by a callback
            return;
        // And so on.
    }
}
2 Likes

Thank you so much for sharing this code - I am so happy that I have finally found a way to do this, after hours of searching.

Apologies for bumping this thread, but is there a way to do this in 2021 without modifying the Juce source code? Obviously this change would break compat with existing projects, or may even have unintended consequences that I have not thought of.

TextEditor has full justification support for single- and multi-line since 12bff68e so there is no need to modify the JUCE code or use workarounds. You can set it with TextEditor::setJustification().

1 Like

Yep I’m aware of this :slight_smile: Should have been more clear: the context is a juce::Slider, where (as far as I can tell) I can’t access the TextEditor from the look and feel methods. (Or am I missing something obvious? I’m looking at JUCE: TextEditor::LookAndFeelMethods Struct Reference )

I would also like to know why you can’t just specify justification to a Slider (for the textBox (and TextEditor, which presumable should follow the same Justification)).

I have been doing it by overriding createSliderTextBox() and fillTextEditorBackground() in a LookAndFeel, which gives you access to the textBox in display state, and textEditor, but it seems there should be an easier way. Like:

Slider::setTextBoxJustification

A basic Slider with a text box currently centers the text, and then jumps it to the left when you edit it (unless I am missing something). I don’t know why the TextEditor would not match the display state by default, even if there was no way to change it.

Or, you could add a Justification parameter to setTextBoxStyle() …

1 Like

Yep this is the problem, and I used the one line fix from @chrisboy2000 (much less work than overriding createSliderTextBox() :slight_smile:)

Edit: rest of my comment was wrong as I was confusing two completely different things (sliders and labels), removed it so it doesn’t cause confusion!

I guess you mean Label::setJustificationType - I wish one for the Slider existed!

Sorry I was looking at completely the wrong part of my code where I was setting the justification for a label / button! It has been a long day…