D
D
Dmitry Korolev2017-02-06 08:53:55
C++ / C#
Dmitry Korolev, 2017-02-06 08:53:55

How to find out the number of lines into which the text is broken with word wrap?

Tried to split with '\n' character, but that didn't work. only one item left. what is the symbol?
and what will the word wrap do if the word does not fit?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Daniil Basmanov, 2017-02-06
@adressmoeistranici

For rendering, the original string does not change. The text has a cachedTextGenerator , from which you can get the lineCount .
UPD: For GUI.Label the number of lines can be calculated using GUIStyle.CalcHeight and GUIStyle.lineHeight .

T
Tom Nolane, 2017-02-06
@tomnolane

TextWrapping - does not split the text sentence itself into parts (i.e. does not insert "\n").
count the number of letters how many fit into 1 line (due to the font and its size - the data can be different ... and probably only manually you can see)
let this number be equal to 100, then (you will need logic, adhesive tape, and a piece quick code)

//вводные данные
int temp_count_chars = 0;
                string temp_predlojenie_stroka = String.Empty;
                int all_chars = 100;
                int colli4estvo_strok = 1;
                string[] arr = textBox.Text.Split(new[] { ' ' }); 
                foreach(string slovo in arr)
                { 
                    if (all_chars >= slovo.Length && (temp_count_chars + slovo.Length) <= all_chars) // если слово меньше или равно общему кол-ву букв с троке и кол-во букв в этой строке вместе с новым словом меньше общей длины строки
                    {
                        if (!string.IsNullOrWhiteSpace(temp_predlojenie_stroka)) { temp_count_chars = temp_predlojenie_stroka.Length + 1;  temp_predlojenie_stroka = "";} //переносное слово с пробелом своим

                        temp_count_chars += slovo.Length + 1; // +1 (это пробел после каждого слова)
                        if (temp_count_chars >= all_chars)
                        {
                            colli4estvo_strok++; //переходим на новую строку
                            if (temp_count_chars != all_chars) // переносим слово
                            {
                                temp_predlojenie_stroka = slovo + " ";
                            }
                            temp_count_chars = 0;
                        }
                    } 
                }
MessageBox.Show("Кол-во строк: " + colli4estvo_strok);

the adhesive tape was not useful - it means they saved ...
p.s. didn't check, did it in haste

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question