Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Search instead for 
Did you mean: 
shess
Level 7

Get Font from Label in BrightScript?

In XML, if I define a label to use a custom font:

<Label>
id="label"
  <Font role = "font" uri = "pkg:/fonts/font.ttf" size = "24" />
</Label>


In BrightScript, how do I get to the actual ifFont object so that I can make calls like GetOneLineWidth?
0 Kudos
10 Replies
squirreltown
Level 8

Re: Get Font from Label in BrightScript?

https://sdkdocs.roku.com/display/sdkdoc/roFont
for custom:
fontreg = createobject("rofontregistry")
fontreg.Register("pkg:/images/fonts/customfont.tff")
Kinetics Screensavers
0 Kudos
shess
Level 7

Re: Get Font from Label in BrightScript?

So - I can't query the node for the font - I have to look in the XML and copy out the font name and the size?  I guess that works, but means every time the XML changes, you have to find any reference in code and update it, too.   Smiley Sad
0 Kudos
rymawby
Level 7

Re: Get Font from Label in BrightScript?

You could actually get the values from the font object using brightscript and then use these values in the getFont call.

So if you had your label like:

<Label
 id="myLabel"
 text="Awesome"
 translation="[300,300]" >
  <Font role = "font" uri = "pkg:/components/fonts/Helvetica.otf" size = "24" />
</Label>


You could get the values of this font by doing something like:

myLabel = m.top.findNode("myLabel")
myFont = myLabel.font
fontURI = myFont.uri
fontSize = myFont.size


You could then use fontURI and fontSize in your roFontRegistry getFont calls.

Hope this helps.
---
Blogging Roku stuff at http://rymawby.com
---
0 Kudos
joetesta
Level 10

Re: Get Font from Label in BrightScript?

Be warned:
A) getOneLineWidth works only in task node
B) Due to firmware upscaling on lower devices such as Roku 3 you will have incorrect values for dimensions not divisible by 3

If you are able to use boundingRect() you will have much easier time and better success.
hope it helps,
Joe
aspiring
0 Kudos
shess
Level 7

Re: Get Font from Label in BrightScript?

Please pardon my slow reply, I got pulled away for a while.

Thank you both for your replies.

At the end of the day, what I'm trying to do is to work around not being able to set the width for a RowList row label.  I have a RowList that is fairly narrow, but might have a long row title.  That label doesn't seem to pay attention to ItemSize.  The only way I can think to work around it is to manually truncate the string if it is going to be too long for that RowList row label - unless there is a better way?

Thanks!
0 Kudos
joetesta
Level 10

Re: Get Font from Label in BrightScript?

I have a few ideas but not sure what would actually work or best suit your needs.
1) Probably easiest to code is a brute-forcish method, check the label's boundingRect().width and compare to the rowlist width.  If it's wider, peel letters off the end until it's not wider.
2) If you'd prefer to have the letters actually chopped in half at the boundary, you could wrap the label in a group with clippingRect 3rd array value equal to the rowlist width.
3) You could also probably update the label fields "ellipsisText" to get rid of the dots and set the width equal to the rowlist, then it won't have a chopped off character and would be slightly less "hack-y" than the first option

hope this helps,
Joe
aspiring
0 Kudos
shess
Level 7

Re: Get Font from Label in BrightScript?

Hi Joe,

Do we have access to the RowList row label directly?
0 Kudos
joetesta
Level 10

Re: Get Font from Label in BrightScript?

"shess" wrote:
Hi Joe,
Do we have access to the RowList row label directly?


looks like yes - rowlist has a field "rowTitleComponentName" that you can use to specify a custom component that is used for each row's label.
In that custom component you can add the code to do the trimming.
aspiring
0 Kudos
shess
Level 7

Re: Get Font from Label in BrightScript?

Hey Joe,

Huh - that seems like an easy fix - I like it!  Is there any way that could be dynamic?  If I create a custom node that inherits from label and set it as that rowlist label component, I'm going to have to hardcode that width into that component, yes?

Thanks, I appreciate your time!
0 Kudos