Label text width

Is there a simple way to measure the displayed width of the text in Label. Obviously there are many ways to do it but is there something almost as simple as:

label->getTextWidth();

…and height for that matter

I would do something like this:

Font f = m_MyLabel.getFont ();
int textWidth = f.getStringWidth(m_MyLabel.getText());

The height would be
int textHeight = f.getHeight();

Hope that is what you are looking for :slight_smile:

  • A

I’ve gone for:

[code]class MeasurableLabel : public Label
{
public:
MeasurableLabel(const String& componentName,
const String& labelText)
: Label(componentName, labelText)
{
}

int getTextWidth()
{		
	return getFont().getStringWidth(getText());
}

float getTextWidthFloat()
{		
	return getFont().getStringWidthFloat(getText());
}

};[/code]

Thanks to martinrobinson. Alternatively as a helper class without branching off Label:

[code]#include <juce.h>

class LabelMeasure
{
public:
LabelMeasure()
{
}

static int getTextWidth(Label* label)
{
return label->getFont().getStringWidth(label->getText());
}

static float getTextWidthFloat(Label* label)
{
return label->getFont().getStringWidthFloat(label->getText());
}
};
[/code]