Editable label's TextEdit centered text


#1

I can make a Label's text centered, however there is no way to make its edit box's text centered, is there?


#2

No, sorry - never got around to adding justification to TextEditors.. It's been requested before, I should do it at some point..


#3

Thanks, that's what I was thinking. A first implementation would be that the editor automagically follows the label's alignment (sounds logic).


#4

i've too have come upon a situation where this would be really nice..


#5

although i'd like right justification 


#6

me too...


#7

I’d really like to see support for Justification added to TextEditor, in particular so that a Label’s TextEditor can match the alignment of the Label itself.

There are lots of threads requesting this… pretty please… :kissing_closed_eyes:


#8

I’m newer to JUCE, but I really would appreciate this feature. Any word on this?


#9

While this would be a really useful feature, you can work around it reasonably well in simple cases and where there is only a single line.

Create your own custom Label subclass and override Label::createEditorComponent() where you can create your own subclass of TextEditor. This text editor needs to be a ComponentListener and TextEditor::Listener and listen to itself in both cases.

In the listener callbacks TextEditor::textEditorTextChanged and ComponentListener::componentMovedOrResized you can measure the text using TextEditor::getTextWidth() and TextEditor::getTextHeight(). Then you can resize the TextEditor so that it is just big enough to hold the line of text and position it wherever you like within the parent.


#10

Thanks @martinrobinson, got it working with your advice.

For my own and other’s future reference (and easy copy and paste), here are the Label and TextEditor subclasses:
I added the KeyListener callback so that it keeps the editor entered while the user types.


TextEditor Justification