Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to fit string in rectangle in OnRender() method?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How to fit string in rectangle in OnRender() method?

    Hello.

    Is there a way to render string, using max height and max width, instead of text size?


    There is a way to create TextLayout, and then measure height and width of string.
    I need to perform a reversed task: set height and width of exact string, and then get size to use it in DrawText() or DrawTextLayout();

    Is it possible?
    fx.practic
    NinjaTrader Ecosystem Vendor - fx.practic

    #2
    Hello fx.practic,

    You are wanting to use the width and height of a rectangle and convert this to a font size (or use this to choose a font size)?

    I'm not aware of a SharpDX method that does this.

    I think you are going to have to create some custom math / algorithm to convert this.
    You might trying try using the length x width of rectangle to get the area, and then look at the width and height of the font from the text layout and see if there is a common ration at different sizes.

    I was looking at the WPF FontSizeConverter, but I'm not certain this will suit your purposes.


    This thread will remain open for any community members that may know of a method that exists for this that I am not aware of.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you, Chelsea.
      I will explore FontSizeConverter class and put results here.

      My current solution that I consider as ugly is to re-create TextLayout in a loop with smaller and smaller font size until it fit my rectangle.
      fx.practic
      NinjaTrader Ecosystem Vendor - fx.practic

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by CarlTrading, 03-31-2026, 09:41 PM
      1 response
      82 views
      1 like
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      43 views
      0 likes
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      64 views
      2 likes
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      68 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      56 views
      0 likes
      Last Post CarlTrading  
      Working...
      X