Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question How to fix TMPro line breaks?

Discussion in 'UGUI & TextMesh Pro' started by Nijamyang, Mar 3, 2023.

  1. Nijamyang

    Nijamyang

    Joined:
    Aug 8, 2012
    Posts:
    31
    I came across a text that was fetched from a website and filled into a tmpro element. When I copy paste the text from the component to somewhere I get line breaks. Does anybody know how to get the line breaks shown correctly?

    Screenshot 2023-03-03 at 12.38.58.png
    copy past component text:

    “Meine drei wichtigsten Anliegen für einen sozialen und klimagerechten Kanton Zürich:


    - Chancengerechtigkeit in der Bildung: Der Kanton muss sicherstellen, dass alle Kinder und Jugendliche von früher Förderung und Schulsozialarbeit profitieren können – unabhängig von Wohnort und Portemonnaie.


    - Es braucht zudem eine fortschrittliche Familienpolitik mit genügenden und bezahlbaren Kita-Plätzen.


    - Die Energiewende für einen klimagerechten Kanton muss weiter vorangetrieben werden.“
     
  2. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Try string.replace("\n", Environment.NewLine)
     
  3. Nijamyang

    Nijamyang

    Joined:
    Aug 8, 2012
    Posts:
    31
    I tried to replace "\n" and tried to replace Environment.NewLine but it did not work, there is no n or newLine in the text.

    Code (CSharp):
    1.         text = text.Replace("\n", "XXXX");
    2.         text = text.Replace(Environment.NewLine, "XXXX");
     
  4. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    upload_2023-3-3_14-3-21.png
    According to Notepad++ there are CRLF's in your string.
    i.e. \r\n

    you could always try it with Regex:

    string result = Regex.Replace(input, @"\r\n?|\n", replacementString);
     
    h1037882434 and Nijamyang like this.
  5. Nijamyang

    Nijamyang

    Joined:
    Aug 8, 2012
    Posts:
    31
    Thanks that did the trick, didn't know about CRLF.
     
  6. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    CR - Carriage Return
    LF - Line Feed

    Together they make up "newline" (though many will simply call LF 'newline').
     
    Nijamyang likes this.