2015-06-07 8 views
6

Sto scrivendo un'applicazione per Windows 10 usando Win2D e sto provando a disegnare una forma che si adatta dinamicamente per adattarsi a qualsiasi testo ci sia dentro.Come calcolare la dimensione di un pezzo di testo in Win2D

Quello che mi piacerebbe fare è calcolare la dimensione di una particolare stringa con un determinato CanvasTextFormat e quindi usarla per impostare la dimensione della forma.

Il mio problema è che non riesco a trovare un modo per capire quanto sarà grande la stringa?

risposta

7

vedi codice qui sotto per calcolare la dimensione richiesta (cercare "theRectYouAreLookingFor")

private void CanvasControl_Draw(CanvasControl sender, CanvasDrawEventArgs args) 
{ 
    CanvasDrawingSession drawingSession = args.DrawingSession; 
    float xLoc = 100.0f; 
    float yLoc = 100.0f; 
    CanvasTextFormat format = new CanvasTextFormat {FontSize = 30.0f, WordWrapping = CanvasWordWrapping.NoWrap};   
    CanvasTextLayout textLayout = new CanvasTextLayout(drawingSession, "Hello World!", format, 0.0f, 0.0f); 
    Rect theRectYouAreLookingFor = new Rect(xLoc + textLayout.DrawBounds.X, yLoc + textLayout.DrawBounds.Y, textLayout.DrawBounds.Width, textLayout.DrawBounds.Height); 
    drawingSession.DrawRectangle(theRectYouAreLookingFor, Colors.Green, 1.0f); 
    drawingSession.DrawTextLayout(textLayout, xLoc, yLoc, Colors.Yellow); 
} 
+0

Ho trovato la proprietà LayoutBounds di CanvasTextLayout di essere più utile per me che drawBounds. Forse anche gli altri lo faranno. –

Problemi correlati