In Windows 8 and Windows Phone, We need to calculate the word width. For example, when displaying, we need to cut unnecessary words ..., this is also essential for data formatting.
The pixel width of a character is related to the font and font size. I am using a clumsy method, that is, removing the textblock.
The details are as follows:
TextBlock tb = new TextBlock ();
tb.FontFamily = new System.Windows.Media.FontFamily ("Microsoft Yahei");
tb.FontSize = 50;
tb.Text = "hello";
System.Diagnostics.Debug.WriteLine ("Width:" + tb.ActualWidth + "Height:" + tb.ActualHeight);
This method is simple, but it is different on Windows 8.
In Win8 MetroProgramThe textblock must be displayed on the UI to obtain its width and height. This is troublesome.
Finally, we found that something can be measured. That is, the rectangle.
Rect rect = new Rect (0, 0, 1024, 1024);
TextBlock tb = new TextBlock ();
tb.FontFamily = new FontFamily ("Microsoft Yahei");
tb.FontSize = 50;
tb.Text = "hello";
tb.Arrange (rect);
System.Diagnostics.Debug.WriteLine ("Width:" + tb.ActualWidth + "Height:" + tb.ActualHeight);