Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
jlfreund
Visitor

Word wrapped DrawText

Hi,

I am using DrawText on an roScreen, and was wondering if there is any utility function to handle word wrapping? Currently, I have to measure and layout each line. I see nice text layout and justification in other screens (like roParagraphScreen) so there must be some internal utility code for this somewhere, but I need the performance and flexibility of drawing on roScreen.
0 Kudos
4 REPLIES 4
RokuMarkn
Visitor

Re: Word wrapped DrawText

There isn't any builtin function that does this, but if it helps, here's one that I wrote.

--Mark


function SplitBlockText(text as String, width as Integer, font as Object) as Object
lines = []
line = 0
while true
while text.Mid(line,1) = " "
line = line + 1
end while
end_line = line
while true
if end_line >= text.Len() then
if end_line > line then lines.Push(text.Mid(line, end_line-line))
return lines
end if
e = EndWord(text, end_line)
if font.GetOneLineWidth(text.Mid(line, e-line), width) >= width then
lines.Push(text.Mid(line, end_line-line))
line = end_line
exit while
end if
end_line = e
end while
end while
end function

function EndWord(text as String, index as Integer) as Integer
for p = index to text.Len()
if text.Mid(p,1) <> " " then exit for
end for
for p = p to text.Len()
if text.Mid(p,1) = " " then exit for
end for
return p
end function
0 Kudos
Komag
Roku Guru

Re: Word wrapped DrawText

I could really use this ability, but I'm not quite sure how to use the functions. I know the basic idea of calling a function, passing parameters, but could you (or someone) show a full example code of these word wrapping functions in use?
0 Kudos
RokuMarkn
Visitor

Re: Word wrapped DrawText

Something like this (untested):

' screen is an roScreen
' font is an roFont
' text is the string you want to draw
' text_color is the color of the text
' xpos,ypos is the position at which you want to draw
' width is the width you want to draw

lines = SplitBlockText(text, width, font)
font_height = font.GetOneLineHeight()
foreach line in lines
screen.DrawText(line, xpos, ypos, font_color, font)
ypos = ypos + font_height
end for
0 Kudos
Komag
Roku Guru

Re: Word wrapped DrawText

Thank you, I'll work on that 🙂
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.