How to get the size (in pixels) of a font in WinRT app?

微笑、不失礼 提交于 2019-12-20 06:25:32

问题


As titled, in .NET 4.5 we have a font class which can give you the Height in pixes, but how about in WinRT?

Is there any API that I can use to get the pixels it uses?


回答1:


Since not even the FormattedText class exists in the .NET API for Windows Store Apps my workaround is to use a TextBlock:

TextBlock dummyTextBlock = new TextBlock();
dummyTextBlock.FontFamily = new FontFamily("Tahoma");
dummyTextBlock.FontSize = 18;
dummyTextBlock.FontStyle = FontStyle.Normal;
dummyTextBlock.FontWeight = FontWeights.Bold;
dummyTextBlock.Text = "X";
dummyTextBlock.Measure(new Size(0,0));
dummyTextBlock.Arrange(new Rect(0,0,0,0));
double width = dummyTextBlock.ActualWidth;
double height = dummyTextBlock.ActualHeight;

That gives you the height (and width) of a text how it would be displayed.



来源:https://stackoverflow.com/questions/15515162/how-to-get-the-size-in-pixels-of-a-font-in-winrt-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!