Forum Discussion

philotas's avatar
philotas
Roku Guru
9 years ago

Centering dynamic Label with background?

Since Labels don't have a background I  put a Label inside a Rectangle.
Setting the label width and height to the ones of the Rectangle I can center the label vertically and horizontally just fine.

My problem is, that the text inside the label can be longer or shorter for different languages and thus horizontal centering won't work anymore.

Any idea how this can be done without setting the dimensions for each language.

<Rectangle
          id="background"
          color="0xFF0000FF"
         width= "300"
         height= "50"
          opacity="1"
        <Label 
            id = "label"
           width = "300"
           height = "50"
            translation = "[0,0]" 
            horizAlign = "center"
            vertAlign = "center"
            text = "MY DYNAMIC TEXT"
            color = "0xFFFFFFFF"
            maxLines= "1"
            wrap = "true"
            numLines = "1"
            font = "font:SmallestSystemFont"
         />
        </Rectangle>

6 Replies

  • Not sure it if will work for your situation, but I use font.GetOneLineWidth(textString, width) 
  • "philotas" wrote:
    My problem is, that the text inside the label can be longer or shorter for different languages and thus horizontal centering won't work anymore.
    Any idea how this can be done without setting the dimensions for each language.

    I don't see the issue here. If you size your rectangl-o-label to the longest text, then all language texts will fit - and they will still be centered because... well the label is told to center the text.
  • "Komag" wrote:
    Not sure it if will work for your situation, but I use font.GetOneLineWidth(textString, width) 

    thanks. will give this a try. Does it work with build in fonts?
  • "EnTerr" wrote:
    I don't see the issue here. If you size your rectangl-o-label to the longest text, then all language texts will fit - and they will still be centered because... well the label is told to center the text.

    You are right, you could set it to the longest string, but it would no look good if you have a very short text in wide area. Just as in HTML it should just take the space it needs so that the padding to borders is always the same.
  • "Komag" wrote:
    Not sure it if will work for your situation, but I use font.GetOneLineWidth(textString, width) 

    Mhh. Cannot get it to work. Is roFontRegistry available in SceneGraph? Doesn't look like it.
    reg = CreateObject("roFontRegistry")
    print "roFontRegistry: "
    print reg

    BRIGHTSCRIPT: ERROR: roFontRegistry: no plugin instance available: .../components/MyComponent.brs(
  • Any idea how this can be done without setting the dimensions for each language.

    I don't do SceneGraph but If you want it to look right, you will have to give the Roku a choice based on the language, there's no avoiding it, either preset or dynamic based on GetOneLineWidth().