Forum Discussion

shess's avatar
shess
Visitor
8 years ago

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?

9 Replies

  • 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.   😞
  • 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
  • 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!
  • 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
  • Hi Joe,

    Do we have access to the RowList row label directly?
  • "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.
  • 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!
  • easy? easy for you to say 🙂
    for sure there's more than one way to make it dynamic but probably the way to go is add a field to the custom component (that as you said, extends label) add a new field like
    <field id="parentWidth" type="float" onChange="trimLabel" alwaysNotify="true"/>


    then in your custom component's brs code you have the function "trimLabel" reference m.top.parentWidth to know what its desired target width is.  make sense?
    oh yeah, you'll still need some way to set this field from the parent...  Will be a little tricky but believe it can be done via content, add a parentWidth field to the contentNode.... or maybe there's an easier way to reference those items... the custom component itself might calculate the width (use boundingRect().width) given the content?