Character boundaries of text field based on parent

▼魔方 西西 提交于 2019-12-13 04:47:50

问题


I have a text field inside a container. I'm wondering is it possible to find the boundaries of each character based on the container and not the text field.

Here is a sample screen shot:

And normal state would be like this: With this I can find the bounds of each character based on text field, But I need it based on the container:

var rect:Rectangle = new Rectangle();
for (var i:int = 0; i < textField.length; i++){
    rect = textField.getCharBoundaries(i);
}

Is there anybody whom has an experience on this?


回答1:


I believe you have to make use of Point conversions.

var rect:Rectangle = new Rectangle();
for (var i:int = 0; i < textField.length; i++){
    rect = textField.getCharBoundaries(i);

    var globalTopLeft:Point = textField.localToGlobal(rect.topLeft);
    var globalBottomRight:Point = textField.localToGlobal(rect.bottomRight);

    var containerTopLeft:Point = container.globalToLocal(globalTopLeft);
    var containerBottomRight:Point = container.globalToLocal(globalBottomRight);

    rect = new Rectangle(containerTopLeft.x,containerTopLeft.y,containerBottomRight.x-containerTopLeft.x,containerBottomRight.y-containerTopLeft.y)
}



回答2:


If the TextField is a child of the container and the TextField is not scaled, you can just do:

rect = textField.getCharBoundaries(i);
rect.x += textField.parent.x;
rect.y += textField.parent.y;


来源:https://stackoverflow.com/questions/19837705/character-boundaries-of-text-field-based-on-parent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!