King Chan
King Chan

Reputation: 4292

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

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?

Upvotes: 0

Views: 1899

Answers (1)

Jürgen Bayer
Jürgen Bayer

Reputation: 3023

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.

Upvotes: 2

Related Questions