在 WPF 中测量文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/632445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Measuring text in WPF
提问by Tony the Pony
Using WPF, what is the most efficient way to measure a large number of short strings? Specifically, I'd like to determine the display height of each string, given uniform formatting (same font, size, weight, etc.) and the maximum width the string may occupy?
使用 WPF,测量大量短字符串的最有效方法是什么?具体来说,我想确定每个字符串的显示高度,给定统一格式(相同的字体、大小、重量等)和字符串可能占用的最大宽度?
采纳答案by Daniel Earwicker
The most low-level technique (and therefore giving the most scope for creative optimisations) is to use GlyphRuns.
最底层的技术(因此为创造性优化提供了最大的空间)是使用 GlyphRuns。
It's not very well documented but I wrote up a little example here:
它没有很好的记录,但我在这里写了一个小例子:
http://smellegantcode.wordpress.com/2008/07/03/glyphrun-and-so-forth/
http://smellegantcode.wordpress.com/2008/07/03/glyphrun-and-so-forth/
The example works out the length of the string as a necessary step before rendering it.
该示例在呈现字符串之前计算出字符串的长度作为必要的步骤。
回答by Mahdi Nameghi
It is very simple and done by FormattedText class! Try it.
它非常简单,由 FormattedText 类完成!尝试一下。
回答by Martin Lottering
In WPF:
在 WPF 中:
Remember to call Measure() on the TextBlock before reading the DesiredSize property.
请记住在读取 DesiredSize 属性之前在 TextBlock 上调用 Measure()。
If the TextBlock was created on-the-fly, and not yet shown, you have to call Measure() first, like so:
如果 TextBlock 是即时创建的,但尚未显示,则必须先调用 Measure(),如下所示:
MyTextBlock.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
return new Size(MyTextBlock.DesiredSize.Width, MyTextBlock.DesiredSize.Height);
In Silverlight:
在银光中:
No need to measure.
无需测量。
return new Size(TextBlock.ActualWidth, TextBlock.ActualHeight);
The complete code looks like this:
完整的代码如下所示:
public Size MeasureString(string s) {
if (string.IsNullOrEmpty(s)) {
return new Size(0, 0);
}
var TextBlock = new TextBlock() {
Text = s
};
#if SILVERLIGHT
return new Size(TextBlock.ActualWidth, TextBlock.ActualHeight);
#else
TextBlock.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
return new Size(TextBlock.DesiredSize.Width, TextBlock.DesiredSize.Height);
#endif
}
回答by TFD
You can use the DesiredSize property on a rendered TextBox to get the height and width
您可以在呈现的 TextBox 上使用 DesiredSize 属性来获取高度和宽度
using System.Windows.Threading;
...
Double TextWidth = 0;
Double TextHeight = 0;
...
MyTextBox.Text = "Words to measure size of";
this.Dispatcher.BeginInvoke(
DispatcherPriority.Background,
new DispatcherOperationCallback(delegate(Object state) {
var size = MyTextBox.DesiredSize;
this.TextWidth = size.Width;
this.TextHeight = size.Height;
return null;
}
) , null);
If you have a large number of strings it may be quicker to first pre-calualte the height and width of every indiviudal letter and symbol in a given font, and then do a calculation based on the string chars. This may not be 100% acurate due to kerning etc
如果您有大量字符串,首先预先计算给定字体中每个单个字母和符号的高度和宽度可能会更快,然后根据字符串字符进行计算。由于字距调整等原因,这可能不是 100% 准确