Get the width of a line of text

Started by legacyblade, June 24, 2010, 05:55:10 pm

Previous topic - Next topic

legacyblade

I'm working on a (very VERY simple) message system for my game. And I've gotten to the hardest part. How do I find out the width of a string of text? It isn't crucial or anything, but I'd hate to have a huge window to display a single sentence (such as "alright..."). Anyone have ANY idea how I'd go about this?

WhiteRose

June 24, 2010, 08:29:55 pm #1 Last Edit: June 24, 2010, 09:20:30 pm by WhiteRose
Quote from: legacyblade on June 24, 2010, 05:55:10 pm
I'm working on a (very VERY simple) message system for my game. And I've gotten to the hardest part. How do I find out the width of a string of text? It isn't crucial or anything, but I'd hate to have a huge window to display a single sentence (such as "alright..."). Anyone have ANY idea how I'd go about this?

Have you looked at Ccoa's UMS? It has a feature that does this. If you examine the code, you'll probably be able to figure out how he (/she/them/it) made it work.

G_G

try this
width = self.bitmap.text_size(string here).width

Or self.contents if you're making a window class

legacyblade

@WhiteRose, Ccoa's UMS is a HUGE script with tons of options, so it's hard to figure out how she did stuff. I already tried looking through hers :P Thanks for the suggestion though.

@g_g, that worked perfectly :D I didn't know it would be that simple. I found a way to scan through each line of code and get which one is the longest (and it's length in one go!)

@text.each{|string|width = [self.bitmap.text_size(string).width, width].max}


(width is used as the variable for use when creating the overall width of the window. Height is easy, as you just need to multiply the amount of lines by however far apart each line is). Cool how simple it is, no? Thanks again :D

@earthine, that'll be a useful reference. Thanks :D