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

width of text.getChildAt

Community Beginner ,
May 26, 2020 May 26, 2020

Copy link to clipboard

Copied

Hi

 

How could i get the with and height of a letter in a text field ?

x and y properties works, while width and heigh not.

 

console.log(target.getChildAt(10).x); This works

console.log(target.getChildAt(10).height); This doesnt.

 

Best regards

Views

470

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
LEGEND ,
May 26, 2020 May 26, 2020

Copy link to clipboard

Copied

Are you thinking that getChildAt() is getting the 10th character of a textfield? No. That tries to get the 10th child symbol of the parent clip.

 

There is no way to get the metrics of individual characters in a textfield.

 

Height isn't working because there is no such property for textfields.

 

Read the documentation: https://www.createjs.com/docs/easeljs/classes/Text.html

 

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 Beginner ,
May 26, 2020 May 26, 2020

Copy link to clipboard

Copied

Thank You for response ClayUUID.

 

Thats correct, im trying to get the coords and measurments of the letters for an effect automation, but its not going so well.

Asuming there will be only a static textfield in a movieclip, getchild does work fine for the purpose it will need to.

But as You pointed out, no real way to get the size of shapes. Mayby something with setBounds would do the trick ? 

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 ,
May 26, 2020 May 26, 2020

Copy link to clipboard

Copied

Hi.

 

You can get the width of a single letter by:

1 - Saving the original text string in a variable or property;

2 - Setting the text field's text property value to the letter you wish;

3 - Using the getMeasuredWidth method to get the width of the text field that now has only one letter;

4 - Restoring the original text using the value stored previously.

 

Like this:

var textField = this.txt;
var originalText = textField.text;
var letterWidth;

textField.text = textField.text[10];
letterWidth = textField.getMeasuredWidth();
textField.text = originalText;

console.log(letterWidth);

 

Please let us know if you have any further questions.

 

Regards,

JC

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 Beginner ,
May 31, 2020 May 31, 2020

Copy link to clipboard

Copied

LATEST

Hi JoãoCésar, Thank You for reply.

 

This is a clever way, and a fine workaround for now. Thank You for this example.

Could this work with a static text field aswell ?

 

Best regards

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