Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoEllipsis=true affects the vertical positioning of the text

I have a label with AutoEllipsis = true and TextAlign = ContentAlignment.MiddleLeft. When I enter a text that is not extending the label width, the text is vertically aligned to the middle of the label.

enter image description here

However, when the text extends the label width the text is not aligned to the middle, but top aligned instead.

enter image description here

Why is it behaving this way, and is there a way to keep the text vertically center aligned?

like image 274
Robin Avatar asked Jun 27 '14 08:06

Robin


1 Answers

I see it. This looks like a limitation in the underlying winapi, DrawTextEx(). Which doesn't get a lot of help from the Label class, it doesn't turn on the DT_SINGLELINE option (aka TextFormatFlags.SingleLine) since it is capable of rendering multiple lines. DrawTextEx() documents that this is required to get vertically centered text (DT_VCENTER). So the real bug is that it shouldn't be centered at all :) Do note that you do get centered text when you grow the label vertically.

The simplest way to work around it is by setting the label's UseCompatibleTextRendering property to True.

like image 188
Hans Passant Avatar answered Nov 05 '22 17:11

Hans Passant