Setting TextButton font size

That’s exactly the reason, why LookAndFeel exists, so you can make it exactly to your needs.

The supplied drawButtonText tries to please everyone, so somebody will always want it differently.

Glad you found your solution.

My point is, I should not have needed to do this, which I did in between replying to you and matkamusic. Yes I understand you can’t please everybody, but I strongly believe the function should have scaled my text. I mean something as simple as making text fit, not talking about making a button that looks like a random animal.

I read the forums daily. As I have no formal training I am hesitant to post. I often find other non-Juce forums very hurtful. People can have ulterior motives that they are not wary of, because it is a faceless exchange. JUCE forums have always been exceptional. I think Jules started a generous culture with his example of a humble great mind.
Daniel and Matkatmusic, you are both also inspirational in your generosity. DKDiveDude I read the JUCE forum daily and I have admired your courage to ask and humbly learn at a phenomenal pace. You have contributed so much in such a short time.
Despite Jules obvious perfectionism, the text button text took some getting used to. I would vote for a small updated text feature request like DKDiveDude’s change.
Peace until the whole world goes up in smoke because of men and their unchecked motivations. I am in Australia where climate change is not real.

2 Likes

Thank you very much. I would not have known what I know now about JUCE, having completed an arcade game, and now developing a synthesizer which I can already make exciting and unique sounds on, had it NOT been for this forum. Most are extremely helpful in their replies, and I give credit to especially @cpr and @daniel. But I said it before and I say it again, if you know your C++ like the palm of your hands, you may not need this forum or at least not much. However if you don’t speak C++ as good as your native language, where mine is Danish, the JUCE tutorials, examples, and documentation for me at least was absolutely not enough as I also had to learn more about C++ along the way. Tutorials and examples were confusing to me due to inconsistency, mixed use of using only header files or both header and cpp files. The documentation, although great, for me and I suspect many others, would would have been awesome if each page had a short example code snippet in the top.

1 Like

A bit of offtopic for my part, but I don’t think it’s good to have multiple new post (1-2 every year every time a newcommer comes) when an older post was already touching the topic and can be continued from there. If there’s something to add or some issue left unresolved it’s interesting to keep with it so everything stays in a single thread (unless it’s an ancient post from 2008, you get it) instead of various dispersed threads linked, plus the original thread participants who probably already came with a satisfactory solution will be notified and probably can help with it.

3 Likes

Thought you ought to know, that I forgot to mention yesterday in my answer, that I did not know the answer (solution) when I revived this thread.! I figured it out after, worked on it in between responding to you.

Original post: “I was wondering if it was possible to set the size of a textbutton’s font without having to create a look and feel class for it. I’ve read everything on this thread:”

Sounds like the answer is “No”, lol.

Not that I know off. So I think you have to implement your own LF class.

Again as I complained about earlier in this thread, it is because there is hard coded values in the function that draw button text. Not in drawButtonText which I earlier in this thread “fixed” to get rid of the hard coded margins, but the remaining hard coded value is in getTextButtonFont, see below.

Font getTextButtonFont (TextButton&, int buttonHeight)
	{
		return { jmin (**16.0f**, buttonHeight * 0.6f) };
	}

So I revised my earlier class and function, so it makes short text bigger, and drawFittedText inside drawButtonText should still manage to scale down for longer text. Try and implement this and let me know if it works for you;


class myLookAndFeel : public LookAndFeel_V4 {
public:
	void drawButtonText (Graphics& g, TextButton& button,
		bool /*shouldDrawButtonAsHighlighted*/, bool /*shouldDrawButtonAsDown*/)
	{
		Font font (button.getHeight () * 0.6f);
		g.setFont (font);
		g.setColour (button.findColour (button.getToggleState () ? TextButton::textColourOnId
			: TextButton::textColourOffId)
			.withMultipliedAlpha (button.isEnabled () ? 1.0f : 0.5f));

		const int yIndent = button.proportionOfHeight (0.1f);
		const int cornerSize = jmin (button.getHeight (), button.getWidth ()) / 2;

		const int leftIndent = cornerSize / (button.isConnectedOnLeft () ?
                  yIndent * 2 : yIndent);
		const int rightIndent = cornerSize / (button.isConnectedOnRight () ? 
                  yIndent * 2 : yIndent);
		const int textWidth = button.getWidth () - leftIndent - rightIndent;

		if (textWidth > 0)
			g.drawFittedText (button.getButtonText (),
				leftIndent, yIndent, textWidth, button.getHeight () - yIndent * 2,
				Justification::centred, 2, 0.5f);
	}
}

You can adjust the “0.6f” value in “Font font (button.getHeight () * 0.6f);” to your needs, for example a bit up for bit larger font size.

Also note that with the above solution, because I bypassed getTextButtonFont, you lose the consistency of uniformly sized text for all buttons.

1 Like