Search code examples
c++textdirectxdirect2ddirectwrite

DirectWrite align text center


I am trying to align center the text I have to draw. I use ID2D1RenderTarget::DrawTextLayout method. The problemn is that if I set the text horizontal alignment to DWRITE_TEXT_ALIGNMENT_LEADING (the default value) the text is drawn proberly, but if I change this value to DWRITE_TEXT_ALIGNMENT_CENTER the text is shifted right.

The example string is

Internal Amazing 
Scupper

Following are the outcomes (the first is alignment leading):

enter image description here enter image description here


Solution

  • My comment as an answer (yeah, guessed right :)):

    Just a quick guess: Did you check that the maxwidth your layout box isn't too broad, so the center would end there at the right?

    The IDWriteTextLayout used by ID2D1RenderTarget::DrawTextLayout method defines a maximum width of the layout box, which determines where the text is centered. It can be manipulated by the methods of the interface (GetMaxWidth and SetMaxWidth).