String.length
只会告诉我字符串中有多少个字符。 (事实上,在Ruby 1.9之前,它只会告诉我有多少字节,这更没有什么用处。)
我真的很想知道一个字符串有多少“en”宽。例如:
'foo'.width
# => 3
'moo'.width
# => 3.5 # m's, w's, etc. are wide
'foi'.width
# => 2.5 # i's, j's, etc. are narrow
'foo bar'.width
# => 6.25 # spaces are very narrow
如果我能获得第一就更好了n
字符串的 en:
'foo'[0, 2.en]
# => "fo"
'filial'[0, 3.en]
# => "fili"
'foo bar baz'[0, 4.5en]
# => "foo b"
如果我能制定整件事的策略就更好了。有些人认为空格应该是0.25en,有些人认为应该是0.33,等等。
您应该使用 RMagick gem 使用您想要的字体渲染“Draw”对象(您可以加载 .ttf 文件等)
代码看起来像这样:
the_text = "TheTextYouWantTheWidthOf"
label = Draw.new
label.font = "Vera" #you can also specify a file name... check the rmagick docs to be sure
label.text_antialias(true)
label.font_style=Magick::NormalStyle
label.font_weight=Magick::BoldWeight
label.gravity=Magick::CenterGravity
label.text(0,0,the_text)
metrics = label.get_type_metrics(the_text)
width = metrics.width
height = metrics.height
您可以在我的按钮制作器中看到它的运行情况:http://risingcode.com/button/everybodywangchungtonite http://risingcode.com/button/everybodywangchungtonite
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)