- Home /
Sensing if a string is multi - lined
Hi, all! I am creating a conversation system, and it is going along quite well. I was wondering if anyone knew how to sense if a string is longer than the space it is contained in. I have a box that is high enough for one line of text across it, and if the string is too long, it runs off the box. I need to make a script that checks to see if the supplied string is longer than x amount of characters, and if so, pop an old school scrollbar on there. I'm pretty sure I can achieve the scroll bar with two buttons and a GUI group, but I would like to know how to sense if a string is multi-lined.
Some images if I'm not being clear here. the first is text that fits, and the second is text that doesn't.
I would be a bit nervous saying "Do I know you?" to a green entity with a rifle next to his hand. :) Cool pics, and helpful ones too.
Answer by flamy · Dec 17, 2011 at 08:50 PM
you can get the length of the string using "Length" property
it works like stringName.Lenghth;
u can calculate the number of character that can contain in the bar using its width and height along with the the size of the font you are using!
so just compare it and u would get what you want!!
The length of the string doesn't help you deter$$anonymous$$e its size (in pixels) when drawn with the GUI.
You need to use GUIStyle.CalcSize to relate the string to the GUIStyle and rectangle.
oh i dont think so anyway GUIStyle.CalcSize would make it easy!!
u could get size in pixels by multiplying the font size u r using to draw the font with the strlen!!
But if the font is size 24, does that mean it is 24 pixels by 24 pixels? If so, the math would suggest that if I have a box that is 350 wide, I could fit fourteen characters across it, when I can actually fit 26.
I would guess the CalcSize and CalcHeight functions exist to save us programmers the headache of considering all the little factors that come into play when mapping a character and a font to a pixel size.
I would imagine there are factors like the spacing between the characters and the style that must come into play.
It's probably similar to the way that GUILayout takes care of all kinds of factors that are a pain in the butt to consider manually (like borders, margins, scrollbars, etc...).
So how would I go about using them? I looked them up in the script reference and I am CLUELESS
Answer by Statement · Dec 17, 2011 at 10:24 PM
Maybe you could use GUIStyle.calcSize? to figure out how large space the text with a given style need? And the scrollbar can be automatic with GUI.BeginScrollView. That is, if you use UnityGUI..
Really? Thanks! I'll try the scroll bar and see if I like it ;)
Your answer
![](https://koobas.hobune.stream/wayback/20220613062152im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
GUI Scroll View 1 Answer
String not showing up in GUI label 1 Answer
Scrolling Text 1 Answer
How to output a file to a single string? 1 Answer
I need help with my pause menu 1 Answer