I need to draw text in a table cell with a fixed width (in pixels) and a fixed number of text lines. If the text is cropped by the rectangle of the cell, it must end with an ellipsis. The problem is that I cannot correctly calculate the rectangle of the text (or the TextRect / DrawText procedure does not work correctly, I'm not sure).
I tried using this method to calculate a text rectangle:
var TextRect: TRect; tm: TEXTMETRIC; ... GetTextMetrics(Canvas.Handle, tm); TextLineHeight := tm.tmHeight + tm.tmExternalLeading; TextRect.Bottom := TextRect.Top + TextLineHeight * NumberOfLines; Canvas.TextRect(TextRect, 'some long long long text', [tfTop, tfLeft, tfEndEllipsis, tfWordBreak]);
The clipping rectangle is calculated correctly, but the ellipsis does not appear.
The ellipsis appears when I reduce the clipping height of the rectangle by 1 pixel:
TextRect.Bottom := TextRect.Top + TextLineHeight * NumberOfLines - 1;
But some pixels of the bottom line of my text are cropped then.
How to do it right?
source share