开发者

Program or method for finding the longest word (pixel wise) from a list?

I'm designing something that requires very precise spacing, so I need to allocate the correct space for each column.

开发者_开发问答I have a huge list of names, very long, and I want to calculate how much space the longest name would take up. Having 'iiii' is different than 'mmmm' even though they have the same amount of characters, so that obviously, depending on the font used, should be taken into account.


If you're on win32, I think GetTextExtent is the API call you're looking for. Other platforms should have similar APIs.


Most platforms (and/or font handling libraries) provide a way to obtain the font-wide maximum advance width, which you could use to allocate column width if you absolutely need to fit. This should be a very inexpensive operation as you're simply looking up a value, then multiplying by the number of characters in the string(s), as opposed to actually rendering the strings.

Some platforms also provide a font-wide average advance width value; if you just need to get "pretty close" you could probably devise a formula that takes the average into account.

Under Win32, GetTextMetrics returns a TEXTMETRIC structure which contains both the max and average. As mentioned above, other platforms should have similar APIs but you'll have to figure that out yourself, or edit your question so we can help you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜