How to make a juce string fixed with a length

How do I fill to a label a string fixed with width of label. If the string is too long, it will auto cut the string and add to the end of the string?
For example: “This is the string verry very long” => “This is the string v…”


Thank @ed95 so much :sunny:

@ed95 Can you kindly tell me how to draw 2 colors of text inside a label?
For example: A label has a string as a title “Name (*):” so how to do if I want * has red color while “Name”, “(”, “)”, “:” are white?

That’s not possible with a Label AFAIK but you can achieve it with a TextEditor and make it behave like a Label by setting it to be read-only and changing some colours. Here’s some code that should do it:

editor.setColour (TextEditor::backgroundColourId, Colours::transparentBlack);
editor.setColour (TextEditor::outlineColourId,    Colours::transparentBlack);
editor.setColour (TextEditor::textColourId, Colours::white);
editor.insertTextAtCaret ("Name (");
editor.setColour (TextEditor::textColourId, Colours::red);
editor.insertTextAtCaret ("foo");
editor.setColour (TextEditor::textColourId, Colours::white);
editor.insertTextAtCaret (")");

Thank you so much @ed95

You could make a custom look and feel for the label and use AttributedString to draw text with multiple colours.

Thank you @Im_Jimmi, I’ll try it :slight_smile:

1 Like

Thank all of you for your answers, I have one more problem with Label here is how to show Unicode text? Please help me this problem. Thanks

If you just enter / copy&paste unicode text into the label, it should simply work.
If you want to write into the label from code, have a look into the tool in “Projucer” -> “Tools” -> “UTF-8 String-Literal Converter”. Type in what you want, and it will come returned as code:

Hope that helps

1 Like

Yes, Thank you @daniel, It will work I think, but I think I won’t be able to do this if I want to show on the label a Unicode-string which will be read from a config file. :frowning:

You can just copy the functionality of that tool into your own code:

juce Strings are always unicode. The stuff above is only relavent if you’re embedding strings in a source code file. If you’re loading one from a text file then just make sure the file itself is UTF8, and use File::loadFileAsString

1 Like

Thank you @Im_Jimmi, @jules I got it