Skip to main content
Known Participant
May 26, 2020
Question

width of text.getChildAt

  • May 26, 2020
  • 4 replies
  • 662 views

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

    This topic is closed to new replies. Start a new post to keep the conversation going.

    4 replies

    JoãoCésar17023019
    Community Expert
    Community Expert
    May 26, 2020

    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

    ed971Author
    Known Participant
    May 31, 2020

    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

    Legend
    May 26, 2020

    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

     

    ed971Author
    Known Participant
    May 26, 2020

    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 ?