如何在UWP Apps中测量文本大小? [英] How can I measure the Text Size in UWP Apps?
本文介绍了如何在UWP Apps中测量文本大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在WPF中,可以使用 FormattedText
In WPF, this was possible using FormattedText
, like this:
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平台中计算文本尺寸呢?
But in UWP this class does not exist any more. So how is it possible to calculate text dimensions in universal windows platform?
推荐答案
在UWP中,您创建了 TextBlock
,设置其属性(例如 Text
, FontSize
),然后调用其 测量
方法并传递无限大小。
In UWP, you create a TextBlock
, set its properties (like Text
, FontSize
), and then call its Measure
method and pass in infinite size.
var tb = new TextBlock { Text = "Text", FontSize = 10 };
tb.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
之后,其 DesiredSize
属性包含TextBlock将具有的大小。
After that its DesiredSize
property contains the size the TextBlock will have.
这篇关于如何在UWP Apps中测量文本大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文