在WPF中,可以使用FormattedText
来实现此功能,如下所示:
private Size MeasureString(string candidate)
{
var formattedText = new FormattedText(
candidate,
CultureInfo.CurrentUICulture,
FlowDirection.LeftToRight,
new Typeface(this.textBlock.FontFamily, this.textBlock.FontStyle, this.textBlock.FontWeight, this.textBlock.FontStretch),
this.textBlock.FontSize,
Brushes.Black);
return new Size(formattedText.Width, formattedText.Height);
}
但是在UWP中这个类已经不存在了。那么在通用Windows平台上如何计算文本大小呢?