locked
Drawing text in a given rectangle RRS feed

  • Question

  • In WPF, is there a way to draw a text string in a Rectangle like the Graphics.DrawString method that takes a rectangle?  I am drawing text at an angle and I don't want to have to figure out exactly where to start drawing the text. I know the rectangle that will hold the text, so I am hoping there is a way to do that. I am working with FormattedText objects.

     

    Gene

     

    Friday, September 7, 2007 8:00 PM

All replies

  • Hello, Firstly, you can add one Label/TextBlock control into Rectangle to display string, in this way you don't need to draw string yourself.

    If you really need to draw it yourself, you can override OnRender method, which is same with OnPaint method in WinForms. Then you will get one DrawingContext object, so you can call DrawingContext.DrawText method to draw string.

    Friday, September 7, 2007 11:24 PM
  • Your information has helped me. Just a small variation on the original subject...

     

    Do you know how to do this?

     

    I have a specific rectangle and text string. I want to find out what Font size will be needed to draw the text inside the rectangle, taking up all the space in the rectangle.

     

    I know I can try calling Measure on the text string with various Font sizes until I come up with the closest one, but is there a special way that I can Measure a text string, giving it the specific rectangle, and having the Font size provided for me?

     

    Thanks.  Gene

     

    Tuesday, September 11, 2007 6:58 PM