Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Damian Day |
public static void DrawAlignedText(this CanvasItem canvas, Font font, string text, float x, float y, float width, float height, Color color, int alignV, int alignH)
{
if (font == null)
return;
var size = font.GetStringSize(text);
if ((width == 0) || (height == 0))
{
width = size.x;
height = size.y;
}
var position = new Vector2();
// 0: left, 1: middle, 2: right
switch (alignV)
{
case 0: position.x = x; break;
case 1: position.x = x + (width - size.x) * 0.5f; break;
case 2: position.x = x + (width - size.x); break;
}
// 0: left, 1: middle, 2: right
switch (alignH)
{
case 0: position.y = y; break;
case 1: position.y = y + (height - size.y) * 0.5f; break;
case 2: position.y = y + (height - size.y); break;
}
canvas.DrawString(font, position, text, color);
}
public static void DrawTextCentered(this CanvasItem canvas, Font font, string text, Rect2 rc, Color color)
{
DrawAlignedText(canvas, font, text, rc.Position.x, rc.Position.y, rc.Size.x, rc.Size.y, color, 1, 1);
}
I would like to be able to align my text to the middle.
Please see above for my current c# code, it will center text horizontally just fine, but it won’t vertically. This code works perfectly fine with other engines, what am I doing wrong with godot please?