如何计算字符数可以贴在标签上? [英] How to calculate number of character can fit on label?

查看:94
本文介绍了如何计算字符数可以贴在标签上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有标签的宽度和高度,通过使用宽度和高度信息,我们如何计算标签上的字符数量?#div class =h2_lin>解决方案

这是不适定的问题。大多数字体不是 monospace 。你可以适应更多l而不是W。没有确定的答案,因为问题的表述无效。但是,对于等宽字体,这个问题是可以解决的,而且非常简单,我不想讨论它:测量一个字母(该方法取决于UI库;您没有指定您正在使用的内容)。一种常见的方法是使用隐藏的自动尺寸标签;你可以写一个字母到它的文字并阅读它的宽度。



对于某些背景:它可能听起来很奇怪,但在一般情况下(使用任意字体),精确测量渲染字符串是一个非常复杂的问题;这种复杂化的原因是字体提示等功能。 (请参阅: https://en.wikipedia.org/wiki/Font_hinting [ ^ ]。)这个问题已在不同的软件论坛中广泛讨论过。



-SA


如果你想确认现有的字符串并查看它是否匹配那么你可以使用系统.windows.form.textrenderer.MeassureString -Method。

此方法使用字体和文本信息,并返回尺寸值。



也许不是直接解决方案...但如果你自定义你的控制(我仍然不知道目标)它可以帮助你...


We have label width and height,by using width and height info ,How can we calculate number of character can feet on label ??

解决方案

This is the ill-posed problem. Most fonts are not monospace. You can fit more 'l' and less "W". There is no definitive answer because the formulation of the problem is invalid. For monospace fonts, however, the problem is solvable and so trivial that I don't want to discuss it much: measure one letter (the method depends on the UI library; you did not specify what you are using). One common method would be using a hidden auto-size label; you can write one letter to its text and read its width.

For some background: it may sound weird, but in general case (with arbitrary font), precise measuring of the rendered string is extremely complicated problem; and the reason for this complication is such features as font hinting. (Please see: https://en.wikipedia.org/wiki/Font_hinting[^].) This problem has been extensively discussed in different software forums.

—SA


If you want to meassure an existing string and look if it matches then you could use the system.windows.form.textrenderer.MeassureString-Method.
This Method works with the information of the font and your text and gives you a Size-Value back.

Perhaps not directly the Solution ... but if you customize your control (I still do not know the Goal) it could help you ...


这篇关于如何计算字符数可以贴在标签上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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