locked
How to measure the exact width and height of a character in a textBox or RichEditbox? RRS feed

  • Question

  • As I'm trying to implement my own editable RichText with overflow containers, I need to know how much space a font would take up in width and height inside a textBox or RichEditBox. And making a textBox with only one char would return the dimension of the UI-Element, not the char itself.

    I'm aware of this thread... which leads to this article
    But this seems a bit of overkill and there is no real guide for directX with c# whatsoever.

    Is there really no other way for measuring the char dimensions? 

    Wednesday, November 13, 2013 12:32 PM

Answers

  • There are samples linked in the article. See http://go.microsoft.com/fwlink/?LinkID=258343 .

    You can use D2D to measure how much space an arbitrary string will take, but that may not match up exactly with a TextBox (&c.) if you don't use the same settings that it does. It should get you close, and it will be exact if you also draw your own text.

    --Rob

    • Marked as answer by SW_Andy Thursday, November 14, 2013 11:47 PM
    Thursday, November 14, 2013 11:14 PM
    Moderator

All replies

  • You found the relevant information. Direct2D is the way to get low level string size information.

    Even finding individual character widths is insufficient to find the correct string width since simply adding the widths doesn't take into account kerning, etc.

    --Rob

    Wednesday, November 13, 2013 7:21 PM
    Moderator
  • Hello Rob, thanks for the reply. The article states: "Also, you can create a Windows Store app with C# and XAML that uses DirectX, if you wrap the DirectX calls in a separate Windows Runtime metadata file." But how exactly Would you do that? Could you shed any light on this? Also I was thinking of having a textframe or specific screenarea and being able to measure how much text would fit in there until the Text is too Long or big for the Frame. I guess that's possible too with direct2d? Andy
    • Edited by SW_Andy Wednesday, November 13, 2013 9:09 PM
    Wednesday, November 13, 2013 9:06 PM
  • There are samples linked in the article. See http://go.microsoft.com/fwlink/?LinkID=258343 .

    You can use D2D to measure how much space an arbitrary string will take, but that may not match up exactly with a TextBox (&c.) if you don't use the same settings that it does. It should get you close, and it will be exact if you also draw your own text.

    --Rob

    • Marked as answer by SW_Andy Thursday, November 14, 2013 11:47 PM
    Thursday, November 14, 2013 11:14 PM
    Moderator