# Get the size in pixels for a string

**URL:** <https://www.ruby-forum.com/t/get-the-size-in-pixels-for-a-string/90019>\
**Category:** Ruby\
**Created:** [March 29, 2006, 4:42pm UTC](https://www.ruby-forum.com/t/get-the-size-in-pixels-for-a-string/90019 "2006-03-29T16:42:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![StSSSSphane\_Thibaud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@StSSSSphane\_Thibaud](https://www.ruby-forum.com/u/StSSSSphane_Thibaud)\
**Post date:** [March 29, 2006, 4:42pm UTC](https://www.ruby-forum.com/t/get-the-size-in-pixels-for-a-string/90019/1 "2006-03-29T16:42:19Z")

</div>

Hi list !

Id like to know the width and height in pixels for a string given a  
font family, size and style.  
How could I do that ?

Thanks,  
Stéphane.

---

<div class="post-metadata">

**Author:** ![StSSSSphane\_Thibaud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@StSSSSphane\_Thibaud](https://www.ruby-forum.com/u/StSSSSphane_Thibaud)\
**Post date:** [March 29, 2006, 5:00pm UTC](https://www.ruby-forum.com/t/get-the-size-in-pixels-for-a-string/90019/2 "2006-03-29T17:00:28Z")

</div>

This is undoubtedly overkill, but:

require ‘tk’

f = TkFont.new(‘Courier 10’)  
width = f.measure(“Hi There!”)  
height = f.metrics.assoc(‘linespace’)[1]
