LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Can I dynamically adjust font size to fit a string control?

Solved!
Go to solution

I have a string control which is updated from a config file. Sometimes if the string is too long then it will wrap around to the next line of the control, but won't be readable (see image). 

ONse_0-1727177704052.png

The size of the control needs to stay as it is, but I'd like the text to always be readable. Now I know I could just reduce the font size so that it fits on 2 lines, but I wanted to keep it as is, because its a good readable size when the string is only one line.

 

I'd like to be able to dynamically adjust the size of the text so that if it uses two or more lines, the font size gets smaller so that it fits into the control. I have only found ways to do this in reverse, i.e. adjusting the size of the control to fit the text, but not adjusting the size of the text to fit the control.

 

Is it possible to do what I'm asking? The String.Text.Width property seems like it should help according to the NI knowledge base, but it stays at the same value and does not change no matter how long the string actually is, so its probably measuring the available space instead - the internal size of the control, as opposed to the external size which is just String.Width, and is only a few pixels bigger.

0 Kudos
Message 1 of 6
(342 Views)

Hi ONse,

 


@ONse wrote:

I'd like to be able to dynamically adjust the size of the text so that if it uses two or more lines, the font size gets smaller so that it fits into the control. I have only found ways to do this in reverse, i.e. adjusting the size of the control to fit the text, but not adjusting the size of the text to fit the control.

Use the Text.Font.Size property!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 6
(326 Views)

@GerdW wrote:

 

Use the Text.Font.Size property!

But this means I'll have to manually determine when the font is too big for the box, and then change it to a pre-selected size.

 

I don't want to hard-code the font size, I just want it to be as big as possible without making parts of the string less visible. So the font size should be relative to the control size

0 Kudos
Message 3 of 6
(320 Views)
Solution
Accepted by topic author ONse

Get Text Rect.vi calculates the size needed for a text with specific font. You would need to iterate with your font size until it fits.

 

RamonG_0-1727181051875.png

 

Message 4 of 6
(315 Views)

You can find out how many lines the text in the indicator is formatted to by using the indicators "Invoke node"->"Get Nth line" and iterate to find last line. Then multiply with line height and compare with indicator height.
I am not sure if Font Size matches line height, but Get Text Rect.vi m which RamonG mentioned should give you correct line heigth.

Added: I read the question again and see that you only want one line. Using "Get Nth line", line 1 should be empty.   

0 Kudos
Message 5 of 6
(286 Views)

@RamonG wrote:

Get Text Rect.vi calculates the size needed for a text with specific font. You would need to iterate with your font size until it fits.

 

RamonG_0-1727181051875.png

 


Exactly what I need, you're a legend thanks very much! I'd have never found that VI on my own 🙂

0 Kudos
Message 6 of 6
(273 Views)