如何计算Win2D一段文字的大小 [英] How to calculate the size of a piece of text in Win2D
本文介绍了如何计算Win2D一段文字的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用Win2D,我想提请其动态调整,以适应任何文字恰好是它的形状编写应用程序的Windows 10。
I am writing an application for Windows 10 using Win2D and I'm trying to draw a shape which scales dynamically to fit whatever text happens to be in it.
我想要做的是制定出一个特定的字符串会多大用给定的CanvasTextFormat,然后用它来设置形状的大小。
What I'd like to do is work out how big a particular string would be with a given CanvasTextFormat and then use that to set the size of the shape.
我的问题是我似乎无法找到工作了字符串将有多大的一种方式?
My problem is I can't seem to find a way of working out how big the string will be?
推荐答案
请参阅下面的代码来计算所需尺寸(寻找theRectYouAreLookingFor)
See code below to calculate the required size (look for "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);
}
这篇关于如何计算Win2D一段文字的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文