Forum Discussion

jlfreund's avatar
jlfreund
Visitor
12 years ago

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.

4 Replies

  • 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
  • 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?
  • 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