A
Arthur Rats
Hi all,
I'm certainly finding this a fun challenge but wanted to know if anyonw
can help
I'm trying to wrap long words that have no spaces, but only the
application is a game and I need to wrap a word based on its true pixel
font width and place this into an array of lines, only that I pass to
method the width in pixels that I need to wrap against.
for a game I;m trying to build (Gosu lib)
font.text_width is the true pixel length of the string
width is in pixels
text is a string
i.e.
def wordwrap(text,width,font)
words = text.split(' ')
lines = [words.shift]
words.each do |word|
if font.text_width("#{lines[-1]} #{word}") < width
lines[-1] << ' ' << word
else
lines.push(word)
end
end
####I have loooooooooong word that needs wrapping
if (text.index(' ').nil? and font.text_width(text) > width)
## I need to chop the string into parts so I have an array
of lines all measuring the "width" pixel width
end
end
Could somebody help, I really have tried many ways and coming from PHP
I'm not too sure of how to do this in Ruby
Thanks!!
Art
I'm certainly finding this a fun challenge but wanted to know if anyonw
can help
I'm trying to wrap long words that have no spaces, but only the
application is a game and I need to wrap a word based on its true pixel
font width and place this into an array of lines, only that I pass to
method the width in pixels that I need to wrap against.
for a game I;m trying to build (Gosu lib)
font.text_width is the true pixel length of the string
width is in pixels
text is a string
i.e.
def wordwrap(text,width,font)
words = text.split(' ')
lines = [words.shift]
words.each do |word|
if font.text_width("#{lines[-1]} #{word}") < width
lines[-1] << ' ' << word
else
lines.push(word)
end
end
####I have loooooooooong word that needs wrapping
if (text.index(' ').nil? and font.text_width(text) > width)
## I need to chop the string into parts so I have an array
of lines all measuring the "width" pixel width
end
end
Could somebody help, I really have tried many ways and coming from PHP
I'm not too sure of how to do this in Ruby
Thanks!!
Art