Calculate the word width in Windows 8 Metro/Windows Phone

Source: Internet
Author: User

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); 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.