# How do I set the font size on TextButtons?

**URL:** <https://forum.juce.com/t/how-do-i-set-the-font-size-on-textbuttons/57808>\
**Category:** Audio Plugins\
**Created:** [September 11, 2023, 6:57pm UTC](https://forum.juce.com/t/how-do-i-set-the-font-size-on-textbuttons/57808 "2023-09-11T18:57:49Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![unclefreddie70](https://avatars.discourse-cdn.com/v4/letter/u/3e96dc/32.png) [@unclefreddie70](https://forum.juce.com/u/unclefreddie70)\
**Post date:** [September 11, 2023, 6:57pm UTC](https://forum.juce.com/t/how-do-i-set-the-font-size-on-textbuttons/57808/1 "2023-09-11T18:57:49Z")

</div>

Hi,

I’m trying to make the font/text inside of a TextButton larger.

Here is what it looks like currently. The A and the B need to be larger so that they fill up the space of the button.

 ![AandB](https://us1.discourse-cdn.com/flex026/uploads/juce/original/3X/a/1/a1e190c87addcab173d8d1c1ac78cc01d647105c.png)

Here is my declaration in PluginEditor.h:

```auto
    TextButton leftChannelButton { "A" };
    TextButton rightChannelButton { "B" };

```

In PluginEditor.cpp, I tried:

```auto
    leftChannelButton.setFont (32.0f);

```

but that failed to compile because:

```auto
PluginEditor.cpp:45:23: error: no member named 'setFont' in 'juce::TextButton'
    leftChannelButton.setFont (32.0f);
    ~~~~~~~~~~~~~~~~~ ^
1 error generated.

```

Can you please put me on the right track?

Best regards,  
Fred

---

<div class="post-metadata">

**Author:** ![stephenk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/stephenk/32/15198_2.png) [@stephenk](https://forum.juce.com/u/stephenk)\
**Post date:** [September 11, 2023, 8:28pm UTC](https://forum.juce.com/t/how-do-i-set-the-font-size-on-textbuttons/57808/2 "2023-09-11T20:28:22Z")

</div>

HI, you need to learn about the Juce LookAndFeel concept, create a custom LookAndFeel, assign it to your TextButtons, and then override LookAndFeel::drawButtonText().

Most of the GUI design will involve working with LookAndFeels.

Tutorial:  
[https://docs.juce.com/master/tutorial\_look\_and\_feel\_customisation.html](https://docs.juce.com/master/tutorial_look_and_feel_customisation.html)

---

<div class="post-metadata">

**Author:** ![DKDiveDude](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dkdivedude/32/7020_2.png) [@DKDiveDude](https://forum.juce.com/u/DKDiveDude)\
**Post date:** [September 11, 2023, 9:13pm UTC](https://forum.juce.com/t/how-do-i-set-the-font-size-on-textbuttons/57808/3 "2023-09-11T21:13:33Z")

</div>

Here is my LookAndFeel solution for that exact problem, because the font size is hard coded into Juce;

```auto
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);
	}
}

```

Optionally you can tweak the ‘0.6’ in this line; Font font (button.getHeight () \* 0.6f);

---

<div class="post-metadata">

**Author:** ![DKDiveDude](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dkdivedude/32/7020_2.png) [@DKDiveDude](https://forum.juce.com/u/DKDiveDude)\
**Post date:** [September 12, 2023, 2:31pm UTC](https://forum.juce.com/t/how-do-i-set-the-font-size-on-textbuttons/57808/4 "2023-09-12T14:31:30Z")

</div>

I forgot to mention the above actually scales the text inside the button to it’s size, less a margin off course which you can tweak with the parameter mentioned.

---

<div class="post-metadata">

**Author:** ![unclefreddie70](https://avatars.discourse-cdn.com/v4/letter/u/3e96dc/32.png) [@unclefreddie70](https://forum.juce.com/u/unclefreddie70)\
**Post date:** [September 14, 2023, 1:41am UTC](https://forum.juce.com/t/how-do-i-set-the-font-size-on-textbuttons/57808/5 "2023-09-14T01:41:16Z")

</div>

Thanks @DKDiveDude. Another question – not sure if it’s a JUCE question or a C++ question, but – why did you comment out the parameter names like that?

> [@DKDiveDude](#):
>
> ```auto
> bool /*shouldDrawButtonAsHighlighted*/, bool /*shouldDrawButtonAsDown*/)
> 
> ```

---

<div class="post-metadata">

**Author:** ![stephenk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/stephenk/32/15198_2.png) [@stephenk](https://forum.juce.com/u/stephenk)\
**Post date:** [September 14, 2023, 1:56am UTC](https://forum.juce.com/t/how-do-i-set-the-font-size-on-textbuttons/57808/6 "2023-09-14T01:56:00Z")

</div>

It’s a c/c++ thing. The arguments are required for the override of the function. However, if you’re not going to use those variables inside the function, you can comment out the names like that and you will then not get compiler warnings about unused variables.

---

<div class="post-metadata">

**Author:** ![cpr2323](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/cpr2323/32/18682_2.png) [@cpr2323](https://forum.juce.com/u/cpr2323)\
**Post date:** [September 14, 2023, 1:32pm UTC](https://forum.juce.com/t/how-do-i-set-the-font-size-on-textbuttons/57808/7 "2023-09-14T13:32:31Z")

</div>

the more modern version of that is the attribute [[maybe\_unused]]

```auto
[[maybe_unused]] bool shouldDrawButtonAsHighlighted, [[maybe_unused]] bool shouldDrawButtonAsDown)

```

obviously it is more verbose, as are the modern type casts, but they express intent better, and can be searched for, etc.

---

<div class="post-metadata">

**Author:** ![stephenk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/stephenk/32/15198_2.png) [@stephenk](https://forum.juce.com/u/stephenk)\
**Post date:** [September 15, 2023, 7:22pm UTC](https://forum.juce.com/t/how-do-i-set-the-font-size-on-textbuttons/57808/8 "2023-09-15T19:22:25Z")

</div>

It’s also worth mentioning you can do this to silence warnings, especially temporarily:

```auto
	void drawButtonText (Graphics& g, TextButton& button,
		bool shouldDrawButtonAsHighlighted, bool shouldDrawButtonAsDown)
	{
        juce::ignoreUnused(shouldDrawButtonAsHighlighted, shouldDrawButtonAsDown);

        // stuff
	}

```

---

<div class="post-metadata">

**Author:** ![kriskeillor](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/kriskeillor/32/20074_2.png) [@kriskeillor](https://forum.juce.com/u/kriskeillor)\
**Post date:** [September 17, 2023, 8:27am UTC](https://forum.juce.com/t/how-do-i-set-the-font-size-on-textbuttons/57808/9 "2023-09-17T08:27:35Z")

</div>

I love opening random threads on the JUCE forum that have nothing to do with my tasks at hand - I always learn something! 🍻
