• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Autofit text

Engaged ,
Feb 27, 2019 Feb 27, 2019

Copy link to clipboard

Copied

Hi, I'm trying to fit font size of text to specific value, but can't find a way to do it. Only solution that works is getting width and height after displaying text on screen, but I need to know it's size before displaying it. I've tried everything: TextField, Text, Label, nothing works. I'm using Flex and Air, but I can use flash controls like TextField by wrapping it in UIComponent.

Code is something like this:

rowHeight = 100;

rowWidth = 300;

var vb:VBox = new VBox;

for (var j:int = 0; j < nrRows; j ++) {

     var hb:HBox = new HBox;

     hb.setStyle("horizontalGap",0);

     var sumWidth:Number = 0;

     while (sumWidth < rowWidht) {

          //here creating some text field

          //set font ACCORDING to rowHeight (considering 'gutter' and other spaces);

          //get ACCURATE text width

          sumWidht += text.width;

     }

     vb.addChild(hb);

}

I've tried many solutions but they give bad size:

var l:Label = new Label;

var fontSize:int = 50;

l.setStyle('fontSize',fontSize);

l.text = txt;

while (l.textHeight > rowHeight) {

fontSize -= 1;

l.setStyle('fontSize',fontSize);

l.text = txt;

}

ar tf:TextField = new TextField();

tf.defaultTextFormat = new TextFormat( "Verdana", rowHeight * 3/4);

tf.text = txt; //set text last

var metric:TextLineMetrics = tf.getLineMetrics(0);

trace('-- txt = ' + txt + '; h = ' + metric.height + '; w = ' + metric.width + '; tf.textHeight = ' + tf.textHeight);

var tfie:TextField = new TextField;

var tfor:TextFormat = tfie.getTextFormat();

tfor.size = 50;

tfie.defaultTextFormat = tfor;

tfie.text = txt;

tfie.setTextFormat(tfor);

var metric:TextLineMetrics = tfie.getLineMetrics(0);

trace('-- txt = ' + txt + '; h = ' + metric.height + '; w = ' + metric.width);

return tfie;

TOPICS
ActionScript

Views

694

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 27, 2019 Feb 27, 2019

Copy link to clipboard

Copied

are you trying to adjust font size to fit a textfield or adjust a textfield to a font size?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Feb 27, 2019 Feb 27, 2019

Copy link to clipboard

Copied

I have HBox row with defined height and I need to put inside some text controls to fit the height of the row. So I need to know what font size will fit this height. Probably I need to take into cosideration coresponding margins, padding, gutter of text control.

After setting fontsize I need to find accurate width of this control.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 04, 2019 Mar 04, 2019

Copy link to clipboard

Copied

Ok, so since you didn't answer it seems there is no way to get text control height before displaying it.

But what would be the best way to get text control width after settng font but before displaying it?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 04, 2019 Mar 04, 2019

Copy link to clipboard

Copied

i wouldn't necessarily say what you're trying to do is not possible.  it's just too complex for me to answer without investing time to solve the problem.

you can always create your textfield assign its properties and font and text etc and analyze it.  if it fails to meet your needs, edit its properties, font, text etc.  because code completes execution before anything is displayed on stage, the user will only see the final product of your code, not the changes.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 04, 2019 Mar 04, 2019

Copy link to clipboard

Copied

Ok, thanks. I've been doing what you say for a week now, and I get wrong numbers. It's like I get 100px width before display and 130px after display. I've tried many diffrent properties but I can't get appropriate value. Well, I'll try to find some kind of workaround. Thanks again.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 05, 2019 Mar 05, 2019

Copy link to clipboard

Copied

LATEST

you're welcome.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines