Microsoft.Maui.Graphics icon indicating copy to clipboard operation
Microsoft.Maui.Graphics copied to clipboard

Measured text does not fit in bounding rectangle

Open rick-palmsens opened this issue 3 years ago • 1 comments

Description The following code first measures a piece of text and then tries to draw that text into a bounding box of the same size. But as you can see in the screenshot, the text doesn't fit and overflows, even though its exact size was measured.

Example

public void Draw(ICanvas canvas, RectF dirtyRect)
{
    var font = new Font("Arial");
    var fontSize = 16.0f;

    // Measure text
    var text = "abcdef ABCDEF vwxyz VWXYZ";
    var textSize = canvas.GetStringSize(text, font, fontSize, HorizontalAlignment.Left, VerticalAlignment.Top);
    var textRect = new Rect(new Point(100, 100), textSize);

    // Draw background 
    canvas.FillColor = Colors.LightBlue;
    canvas.FillRectangle(textRect);

    // Draw text
    canvas.Font = font;
    canvas.FontSize = fontSize;;
    canvas.DrawString(text, textRect, HorizontalAlignment.Left, VerticalAlignment.Top, TextFlow.OverflowBounds);
}

Screenshots image

Versions

<ItemGroup>
  <PackageReference Include="Microsoft.WindowsAppSDK" Version="1.0.3" />
  <PackageReference Include="Microsoft.Graphics.Win2D" Version="1.0.3.1" />
  <PackageReference Include="Microsoft.Maui.Graphics.Win2D.WinUI.Desktop" Version="6.0.300-rc.3.1336" />
</ItemGroup>

rick-palmsens avatar May 12 '22 12:05 rick-palmsens

Note there's some related discussion in #279

swharden avatar May 12 '22 13:05 swharden