TextEditor Alignment

Hi,

 

seems that this is a subject that was already posted before, but i could not see in the posts any solution.

How can I make a TextEditor component to have the text align right or centered, since by default if left alignment.

 

Thanks,

Paulo

As far as I know there is no way to do this. If you only need a basic texteditor then you could draw one yourself, maybe you could use the paintOverChildren() method along with a drawFittedText(). But if it's a fully functional editor you need, with text alignment, I don't think that is possible yet.

You can use the setJustification() method.

PropertyTextEditor.h

#pragma once

#include <JuceHeader.h>

class PropertyTextEditor : public juce::TextEditor
{
public:
    PropertyTextEditor(const juce::String& name);
    ~PropertyTextEditor() override = default;
    
    void paint(juce::Graphics& g) override;
    void paintOverChildren(juce::Graphics& g) override;
    
private:
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(PropertyTextEditor)
};

PropertyTextEditor.cpp

#include "PropertyTextEditor.h"

PropertyTextEditor::PropertyTextEditor(const juce::String& name) {
    setBounds(0, 0, 104, 31);
    auto typeface = juce::Typeface::createSystemTypefaceFor(Assets::RobotoMonoRegular_ttf, Assets::RobotoMonoRegular_ttfSize);
    juce::FontOptions options(typeface);
    juce::Font font(options);
    font.setHeight(21.f);
    setFont(font);
    setColour(textColourId, juce::Colour(0xfffff95e));
    applyFontToAllText(font);
    setColour(highlightColourId, juce::Colour(0xfffff95e));
    setColour(highlightedTextColourId, juce::Colour(0xff111111));
    setJustification(juce::Justification::topRight);
    setCaretVisible(true);
    int caretColourId = 0x1000204;
    setColour(caretColourId, juce::Colour(0xff0084ff));
    InputFilter *filterNumbersOnlyLengthFour = new LengthAndCharacterRestriction(4, "0123456789");
    setInputFilter(filterNumbersOnlyLengthFour, true);
}

void PropertyTextEditor::paint(juce::Graphics& g) {
    g.setColour(juce::Colour(0xff444444));
    g.fillRoundedRectangle(0, 0, getWidth(), getHeight(), 5);
}

void PropertyTextEditor::paintOverChildren(juce::Graphics& g) {
    
}

1 Like

This actually works very well. Some additional adjustment with the indents and voila.

This is what i needed.

class CustomLabel : public juce::Label {
	public:
		CustomLabel () = default;
		juce::TextEditor* createEditorComponent () override;

	private:
		JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CustomLabel)
		};

juce::TextEditor* CustomLabel::createEditorComponent () {
	auto editor = std::make_unique<juce::TextEditor> ();
	editor->setJustification (juce::Justification::Flags::centredRight);
	editor->setFont (getFont ());
	editor->setIndents (0, 0);
	return editor.release ();
}
1 Like