在 WPF 中测量文本 [英] Measuring text in WPF

查看:15
本文介绍了在 WPF 中测量文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 WPF,测量大量短字符串的最有效方法是什么?具体来说,我想确定每个字符串的显示高度,给定统一的格式(相同的字体、大小、粗细等)以及字符串可能占据的最大宽度?

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?

推荐答案

最底层的技术(因此为创意优化提供了最大的空间)是使用 GlyphRuns.

The most low-level technique (and therefore giving the most scope for creative optimisations) is to use 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/

该示例计算出字符串的长度,这是渲染之前的必要步骤.

The example works out the length of the string as a necessary step before rendering it.

这篇关于在 WPF 中测量文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆