Jump to content

Size of Text with Font


photo

Recommended Posts

Posted

Hello,

 

I would like to determine the size of a piece of text rendered using a given font.

 

To do this, you would need this sort of information:

http://download.oracle.com/javase/1.4.2/docs/api/java/awt/font/LineMetrics.html

 

I'd like to do this because I want to position my WidgetLabel consistently, regardless of the font size.

 

If there is an alternative approach, that would be welcome, as well.

 

 

 

 

Michael

Posted

Hi

 

If you want the size in pixel of the Text, you can do that :

 

WidgetLabel txt = new WidgetLabel(gui,"test");

txt.setFont("Arial.ttf");

txt.setFontSIze(48);

txt.arrange();

int size = txt.getWidth();

 

WIth that you have the size in pixel of WIdgetLabel

Posted

I assumed the WidgetLabel width would be independent of the text size. For example, setting a small width on the Label would cause the text to truncate. Is it safe to assume that any time I modify the text, the WidgetLabel will resize to the minimum size needed to fit the text in view?

I could envision a number of situations where such resizing may not be desirable. For example, if you count on the WidgetLabel to have a fixed width in order to enforce a GUI structure, regardless of the content of the label.

Posted

WidgetLabel is always resized to fit all text that was set for it. If text wrapping flag is set, WidgetLabel will be optimally resized to fit its width.

 

Restricting the size of WidgetLabel is not a problem. It can be done pretty easily: create WidgetVBox and add WidgetLabel with the overlap flag to it. After that, you only need to set vbox.setStencil(1) for it to scissor off everything outside its size and set vbox width together with height.

×
×
  • Create New...