You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@flex.apache.org by chris_d_k <ck...@christiankiefer.de> on 2016/04/07 14:46:33 UTC

Scale font size of text to fit into a label with fixed dimensions?

I have 4 labels with have the same dimensions (width="200", maxHeight="100")
and get 4 different texts for the labels.

At least I want that every text fits into the labels dimensions and that all
texts have the same fontSize (the fontsize of the label with most text in
it).

Currently I get the minimim size by testing the maximum fontSize for every
label, compare them and taking the smallest. 

Is there a better way to get the maximum font size which will fit to a label
as below? 

That's my way to get the maximum font size inside the labels parent
component (s:Group):

    // somewhere in updateDisplayList 
    textLabel.setStyle("fontSize", 52);
    while(isResizeNecessary())
    {
	setSmallerFontSizeAndRevalidate();
    }

    // other methods
    private function isResizeNecessary():Boolean
		{
                        var FONT_SIZE_STEPS:int = 2;
			var isTextLabelToHeight:Boolean = textLabel.getPreferredBoundsHeight() >
getMaxHeightForTextLabel();
			var isTextDownscaleable:Boolean = textLabel.getStyle("fontSize") >
FONT_SIZE_STEPS;
			return isTextLabelToHeight && isTextDownscaleable;
		}

		private function setSmallerFontSizeAndRevalidate():void
		{
                        var FONT_SIZE_STEPS:int = 2;
			textLabel.setStyle("fontSize", textLabel.getStyle("fontSize") -
FONT_SIZE_STEPS);
			textLabel.invalidateProperties();
			textLabel.invalidateSize();
			textLabel.invalidateDisplayList();
			textLabel.validateNow();
			invalidateDisplayList();
		}






--
View this message in context: http://apache-flex-users.2333346.n4.nabble.com/Scale-font-size-of-text-to-fit-into-a-label-with-fixed-dimensions-tp12424.html
Sent from the Apache Flex Users mailing list archive at Nabble.com.

Re: Scale font size of text to fit into a label with fixed dimensions?

Posted by "rodolpho@essencialcode.com.br" <ro...@essencialcode.com.br>.
Try to use TextField class. The TextField.textWidth property gives you a
width text in pixels in TextField.text propety...



--
View this message in context: http://apache-flex-users.2333346.n4.nabble.com/Scale-font-size-of-text-to-fit-into-a-label-with-fixed-dimensions-tp12424p12426.html
Sent from the Apache Flex Users mailing list archive at Nabble.com.

Re: Scale font size of text to fit into a label with fixed dimensions?

Posted by Evyatar Ben Halevi-Arbib <ev...@gmail.com>.
I was once in a similar situation and did the following trick on the
label's updateComplete event -

if(myLabel.isTruncated){
myLabel.setStyle("fontSize", myLabel.getStyle("fontSize") - 2);
}

Of course you would need to also set the value for maxDisplayedLines.

Good luck,
Evyatar

On Thu, Apr 7, 2016 at 3:46 PM, chris_d_k <ck...@christiankiefer.de> wrote:

> I have 4 labels with have the same dimensions (width="200",
> maxHeight="100")
> and get 4 different texts for the labels.
>
> At least I want that every text fits into the labels dimensions and that
> all
> texts have the same fontSize (the fontsize of the label with most text in
> it).
>
> Currently I get the minimim size by testing the maximum fontSize for every
> label, compare them and taking the smallest.
>
> Is there a better way to get the maximum font size which will fit to a
> label
> as below?
>
> That's my way to get the maximum font size inside the labels parent
> component (s:Group):
>
>     // somewhere in updateDisplayList
>     textLabel.setStyle("fontSize", 52);
>     while(isResizeNecessary())
>     {
>         setSmallerFontSizeAndRevalidate();
>     }
>
>     // other methods
>     private function isResizeNecessary():Boolean
>                 {
>                         var FONT_SIZE_STEPS:int = 2;
>                         var isTextLabelToHeight:Boolean =
> textLabel.getPreferredBoundsHeight() >
> getMaxHeightForTextLabel();
>                         var isTextDownscaleable:Boolean =
> textLabel.getStyle("fontSize") >
> FONT_SIZE_STEPS;
>                         return isTextLabelToHeight && isTextDownscaleable;
>                 }
>
>                 private function setSmallerFontSizeAndRevalidate():void
>                 {
>                         var FONT_SIZE_STEPS:int = 2;
>                         textLabel.setStyle("fontSize",
> textLabel.getStyle("fontSize") -
> FONT_SIZE_STEPS);
>                         textLabel.invalidateProperties();
>                         textLabel.invalidateSize();
>                         textLabel.invalidateDisplayList();
>                         textLabel.validateNow();
>                         invalidateDisplayList();
>                 }
>
>
>
>
>
>
> --
> View this message in context:
> http://apache-flex-users.2333346.n4.nabble.com/Scale-font-size-of-text-to-fit-into-a-label-with-fixed-dimensions-tp12424.html
> Sent from the Apache Flex Users mailing list archive at Nabble.com.
>

Re: Scale font size of text to fit into a label with fixed dimensions?

Posted by Nemi <ne...@gmail.com>.
Check out  TextlineMetrix
<http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/TextLineMetrics.html>  
and  FontMetrics
<http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/engine/FontMetrics.html> 
. Also  Calculating Text Width In ActionScript And Flex
<http://stackoverflow.com/questions/2916919/calculating-text-width-in-actionscript-and-flex>  
answers could help you to find a better solution. And  Scaling Label text
size to fit available area
<http://blogs.adobe.com/flexdoc/2009/10/scaling_label_text_size_to_fit.html> 
.



--
View this message in context: http://apache-flex-users.2333346.n4.nabble.com/Scale-font-size-of-text-to-fit-into-a-label-with-fixed-dimensions-tp12424p12592.html
Sent from the Apache Flex Users mailing list archive at Nabble.com.