如何在 UWP 应用中测量文本大小? [英] How can I measure the Text Size in UWP Apps?
本文介绍了如何在 UWP 应用中测量文本大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 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 应用中测量文本大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文