`graphics->drawTextInRect` alignment across multiple lines?

I feel like I can't be the first person to have noticed this, but not seeing other reports in my searching! Sorry if I've missed something. I'm currently building against 2.7.3.

The C graphics->drawTextInRect function added in 2.6.0 doesn't seem to be applying text alignment for multiple lines correctly. Is this expected behavior? This code renders how I'd expect:

const char *text = "hello,\nthis is a test!";
int textLen = (int)strlen(text);

pd->graphics->drawTextInRect(text, textLen, kUTF8Encoding, 0, 0, 400, 80, kWrapWord, kAlignTextLeft);
pd->graphics->drawTextInRect(text, textLen, kUTF8Encoding, 0, 80, 400, 80, kWrapWord, kAlignTextCenter);
pd->graphics->drawTextInRect(text, textLen, kUTF8Encoding, 0, 160, 400, 80, kWrapWord, kAlignTextRight);

However, changing the string to "this is a test,\nhello!" renders center and right alignment with only the first line properly aligned. I get the same behavior regardless of the passed wrap mode:

Another example with three lines:

2 Likes

I think I’m having a similar problem with the Lua SDK and graphics.drawText(). I’m using table.concat() to join some values together by "\n” and then trying to print them right-aligned in a rectangle.

I was having this problem with graphics.drawText() but now I tried graphics.drawTextInRect() where the text contains newlines and I want to draw it right-aligned, and it’s doing what I expect it to.

This is what I was attempting to do to right-align a list of numbers (with results)

gfx.drawRect(0, 150, 85, 50)
gfx.drawText("21\n22", 0, 150, 85, 50, nil, nil, gfx.kWrapCharacter, gfx.kAlignRight)

But drawRectInText works correctly:


  gfx.drawRect(0, 150, 85, 50)
  gfx.drawTextInRect("21\nasd22", 0, 150, 85, 50, nil, nil, kTextAlignment.right)

This is on SDK 3.0.2.