How to make a juce string fixed with a length


#1

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…”


#2

Label::setMinimumHorizontalScale()?


#3

Thank @ed95 so much :sunny:


#4

@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?


#5

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 (")");

#6

Thank you so much @ed95


#7

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


#8

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


#9

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


#10

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


#11

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:


#12

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


#13

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


#14

Thank you @Im_Jimmi, @jules I got it