PDA

View Full Version : How to center TextSprite in DrawComponent



Theocritus
28 Sep 2014, 9:41 PM
I tried to use setTextAnchor() and setTextBaseline() to align TextSprite,
when my friend notice I used those two method, he think I didn't read API carefully.

Is there a correct or better way to align TextSprite in DrawComponent?



DrawComponent canvas = new DrawComponent(100, 100);
TextSprite text = new TextSprite("110");
text.setTextAnchor(TextAnchor.MIDDLE);
text.setTextBaseline(TextBaseline.BOTTOM);
text.setFontSize(50);
text.setX(50); // half of canvas width;
text.setY(67.5); // half of canvas height + (fontSize * 0.7 / 2);
text.setFill(RGB.BLACK);
canvas.addSprite(text);


About the TextBaseline.BOTTOM and (fontSize * 0.7 / 2):
The position using TextBaseline.TOP and TextBaseline.MIDDLE seems weird,
only the TextBaseline.BOTTOM really makes word's cap height's bottom attach specific y position.
So I use TextBaseline.BOTTOM and fontSize to adjust TextSprite's position.