Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proper multi line formating #34

Open
YehudaK opened this issue Aug 5, 2019 · 0 comments
Open

proper multi line formating #34

YehudaK opened this issue Aug 5, 2019 · 0 comments

Comments

@YehudaK
Copy link

YehudaK commented Aug 5, 2019

I already sent you the code (via assetstore.unity.com) of small component which formats text in correct multi line and also does that in Editor to speed up the execution time. However, it also supports formatting from code. I did it with regular Text component but I believe there will be no big difference for TextMashPro

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using ArabicSupport;
using System.Linq;

[RequireComponent(typeof(Text))]
[ExecuteInEditMode]
public class ArabicLineFixer : MonoBehaviour {
  [TextArea]
  public string ArabicText;

  public void SetArabicText(string text) {
    this.ArabicText = text;
    StartCoroutine(FixLineOrderCoroutine());
  }

  void OnValidate() {
    StartCoroutine(FixLineOrderCoroutine());
  }

  IEnumerator FixLineOrderCoroutine() {
    Text textComponent = this.GetComponent<Text>();
    List<string> resultText = new List<string>();
    RectTransform rt = textComponent.GetComponent<RectTransform>();
    List<string> paragraphList = ArabicText.Split('\n').ToList();

    foreach (string paragraph in paragraphList) {
      textComponent.text = ArabicFixer.Fix(paragraph, false, false);
      TextGenerationSettings tgs = textComponent.GetGenerationSettings(rt.rect.size);

      if (textComponent.text.IndexOf(' ') < 0) {
        resultText.Add(textComponent.text);

      } else {
        List<string> lineList = new List<string>();
        List<string> wordList = textComponent.text.Split(' ').ToList();
        string singleLine = "";

        while (wordList.Count > 0) {
          string singleWord = wordList[wordList.Count - 1];
          wordList.RemoveAt(wordList.Count - 1);

          if (textComponent.cachedTextGenerator.GetPreferredWidth(singleWord + ' ' + singleLine, tgs) > rt.rect.width) {
            lineList.Add(singleLine);
            singleLine = singleWord;
          } else {
            singleLine = (singleLine != "") ? singleWord + ' ' + singleLine : singleWord;
          }
        }

        if (singleLine.Length > 0)
          lineList.Add(singleLine);

        resultText.Add(String.Join(Environment.NewLine, lineList.ToArray()));
      }

      if (!Application.isEditor)
        yield return new WaitForEndOfFrame();
    }

    textComponent.text = String.Join(Environment.NewLine, resultText.ToArray());
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant