How to return the length of a String
#1
As the title says how do I return the length of a string.

I have a fixed size label that I want to use a different font size for depending on the length of the string that will be displayed.

Thanks
Reply
#2
Not possible unless you can make a script to do it.
Reply
#3
In Kodi Jarvis and later, the InfoBoolean Container(2010).HasNext can be used on a textbox control to tell if it has more text to be displayed; if it is "false" then the text with the given font fully fits in the defined size of the textbox. Using this, several textbox controls with varying fonts can be laid in the same position and visibility can be set based on the values of "HasNext" to display only one at a time. This won't directly give you the length of the string but it will let you style based on how well the text actually fits. The example below uses a label control for the smallest font, so that if the text is still too long it can be truncated with an ellipsis or scroll horizontally if you like.

xml:

<control type="textbox" id="2010">
    <label>$VAR[ListItemTitle]</label>
    <width>400</width>
    <height>70</height>
    <font>titleL</font>
    <!-- Not visible if this font size is too big -->
    <visible>!Container(2010).HasNext</visible>
</control>
<control type="textbox" id="2011">
    <label>$VAR[ListItemTitle]</label>
    <width>400</width>
    <height>70</height>
    <font>titleM</font>
    <!-- Can be visible if the next largest font size is too big -->
    <visible>Container(2010).HasNext + !Container(2011).HasNext</visible>
</control>
<control type="textbox" id="2012">
    <label>$VAR[ListItemTitle]</label>
    <width>400</width>
    <height>70</height>
    <font>titleS</font>
    <!-- Can be visible if the next largest font size is too big -->
    <visible>Container(2011).HasNext + !Container(2012).HasNext</visible>
</control>
<control type="label">
    <label>$VAR[ListItemTitle]</label>
    <width>400</width>
    <height>70</height>
    <font>titleSS</font>
    <!-- Visible if all other fonts are too big -->
    <visible>Container(2012).HasNext</visible>
</control>
Reply
#4
Thanks, that actually works better than relying on the string length, as two different strings with the same character length could actually be different sizes depending on the proportional size of the characters.
Reply
#5
Interesting solution, @rmrector.
Reply
#6
(2018-11-23, 00:52)YggdrasiI Wrote: Interesting solution, @rmrector.

Hi @rmrector or anyone else who has this working. I'm trying to use your solution above for some variable length labels. But it's just not working. As a test, I've added the following textbox control directly into one of my viewtype files, it is not contained within an itemlayout or focuslayout or anything else. It's just floating as a separate control. When I make visible=true, it shows up and the text is clearly cut off indicating HasNext should be true. But as soon as I add Container(2017).HasNext as the visibility condition it stops working.

I've already checked it I use that ID anywhere else and I couldn't find it, I also tried a few different ones and even Container.HasNext for good measure. But no luck. Any ideas what I'm doing wrong?


xml:

            <control type="textbox" id="2017">
                <width>100</width>
                <height>70</height>
                <font>List_Title_Focused</font>
                <textcolor>white</textcolor>
                <label>qwertyioup</label>
                <visible>Container(2017).HasNext</visible>
            </control>

This is what the control looks like when visible = true, just to show it should definitely be true for hasnext

Image
Reply
#7
That's one word not a string (more than one word) so it won't wrap to another line.
Reply
#8
(2020-05-08, 17:05)Hitcher Wrote: That's one word not a string (more than one word) so it won't wrap to another line.

Ahh I see, thanks for explaining this, I never would have guessed that otherwise!
Reply

Logout Mark Read Team Forum Stats Members Help
How to return the length of a String0