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: 
philotas
Roku Guru

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>
0 Kudos
6 REPLIES 6
Komag
Roku Guru

Re: Centering dynamic Label with background?

Not sure it if will work for your situation, but I use font.GetOneLineWidth(textString, width) 
0 Kudos
EnTerr
Roku Guru

Re: Centering dynamic Label with background?

"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.
0 Kudos
philotas
Roku Guru

Re: Centering dynamic Label with background?

"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?
0 Kudos
philotas
Roku Guru

Re: Centering dynamic Label with background?

"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.
0 Kudos
philotas
Roku Guru

Re: Centering dynamic Label with background?

"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(
0 Kudos
squirreltown
Roku Guru

Re: Centering dynamic Label with background?

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(). 
Kinetics Screensavers
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.