Search code examples
rokubrightscript

Checking for button title overflows in Brightscript


In Roku's library, to add a button to a spring board screen, you use the method (see doc):

AddButton(buttonID as Integer, title as String) as Boolean

If title overflows the button width, it will automatically truncate the text and append "...". But is it possible to programmatically check whether there was an overflow or not?


Solution

  • There isn't a nicely way to check that, but as workaround you have two options: check the string length for a maximum safe length or use GetOneLineWidth() method.

    For the second option, you must know button's width, font family and fontsize, eg:

    button_width = 450    'px
    
    reg = CreateObject("roFontRegistry")    
    arialFont = reg.GetFont("Arial", reg.GetDefaultFontSize(), false, false)
    title_width = arialFont.GetOneLineWidth(titleString, 1280)    'maxWidth set to screen width
    
    if title_width > button_width then
        'do your logic here
    end if