09-24-2024 06:40 AM
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).
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.
Solved! Go to Solution.
09-24-2024 07:11 AM
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!
09-24-2024 07:22 AM
@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
09-24-2024 07:30 AM
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.
09-24-2024 08:14 AM - edited 09-24-2024 08:22 AM
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.
09-24-2024 08:24 AM
@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.
Exactly what I need, you're a legend thanks very much! I'd have never found that VI on my own 🙂