我正在使用 Win2D 为 Windows 10 编写一个应用程序,并且尝试绘制一个可动态缩放的形状以适应其中的任何文本。
我想做的是计算出给定 CanvasTextFormat 的特定字符串有多大,然后用它来设置形状的大小。
我的问题是我似乎无法找到计算字符串有多大的方法?
请参阅下面的代码来计算所需的大小(查找“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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)