首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将Visual转换为逻辑RTL的C#算法

将Visual转换为逻辑RTL的C#算法
EN

Stack Overflow用户
提问于 2015-08-30 14:38:45
回答 1查看 966关注 0票数 3

我正在用Unity3d编写一个项目,使用C#脚本,用希伯来语和英语编写字符串。Unity3d的问题是在编写UI文本时不支持RTL语言(如希伯来语)。

问题如下:原文:מערכתהעטלףהינהמערכת,אשרמתבססתעלטכנולוגיתה-探地雷达-ראדארחודרקרקע.

统一: GPR -התיגולונכטלעתססבתמרשא,תכרעמהניהףלטעהתכרעמ.עקרקרדוחראדאר-(探地雷达)

我正试图破解文本被打印到文本框上的顺序,现在已经有一个月了,我无法正确地得到它。我组成了以下功能:

代码语言:javascript
复制
    public string CorrectText (string text)
{
    string result = "";
    string temp = "";
    string charListString = "";
    List<Char> charList = new List<char> ();
    char[] charArray = text.ToCharArray ();

    Array.Reverse (charArray);

    //go through each char in charArray
    for (int x = 0; x <= charArray.Length -1; x++) {
        //if the current char we're examing isn't a Space char -
        if (!char.IsWhiteSpace (charArray [x])) {
            //add it to charList
            charList.Add (charArray [x]);
        } else { //if the current char we're examing is a Space char -
            charListString = new string (charList.ToArray ());

            //if charListString doesn't contains English or Numeric chars -
            if (!Regex.IsMatch (charListString, "^[0-9a-zA-Z.,()-]*$")) {
                //go through each char in charList 
                for (int y = 0; y <= charList.Count - 1; y++) {
                    //add the current char to temp as is (not flipped)
                    temp += charList [y];
                }

                //add temp to result
                if (x < charArray.Length - 1)
                    result += temp + " ";
                if (x == charArray.Length - 1)
                    result += temp;

                //clear charList and temp
                charList.Clear ();
                temp = "";
            } else { //if temp contains English or Numeric chars -
                //go through each char in charList - This flipps the order of letters
                for (int y = charList.Count - 1; y >= 0; y--) {
                    //add the current char to temp
                    temp += charList [y];
                }

                //add temp to result
                if (x < charArray.Length - 1)
                    result += temp + " ";
                if (x == charArray.Length - 1)
                    result += temp;
                //clear charList and temp
                charList.Clear ();
                temp = "";
            }
        }

    }

    return result;
}

这几乎解决了问题,但文本是自下而上编写的,例如:

输入:מערכתהעטלףהינהמערכת,אשרמתבססתעלטכנולוגיתה-探地雷达-ראדארחודרקרקע。

输出:(探地雷达)-ראדארחודרקרקע。מערכתהעטלףהינהמערכת,אשרמתבססתעלטכנולוגיתה-探地雷达

有时,括号在句子中被混在一起,表现为:)探地(rad-ראדארחודרקרקע)。

我在网上找到了一个工具,可以将可视化希伯来语转化为逻辑希伯来语,当我复制翻转文本以实现统一时,它就像魔法一样有效!

但我在网上找不到任何有用的信息,说明如何用C#制作自己的脚本。

EN

回答 1

Stack Overflow用户

发布于 2015-12-09 19:55:29

我设法克服了这一切。有点像:

  1. 将字符串分配到text元素中。
  2. 强制画布使用Canvas.ForceUpdateCanvases()更新,或者使用另一种方法。
  3. 获取文本组件的缓存文本生成器(而不是布局)。如果您没有强制更新或放弃并等待下一个框架,它将对您撒谎;所以不要跳过步骤2。
  4. 使用缓存文本生成器的getLines来获取文本包装的信息;它保存每行开始的索引。
  5. 取这些值的最大差值,那么在包装前,每行可能有多少字符。
  6. 使用上一步中找到的行长度,在翻转字符之前手动将'\n‘(新行)插入原始字符串中!
  7. 对包装好的字符串执行反转处理。如果你以不同的顺序去做这些事情,你最终会发现第一行是最短的,而不是最后一行。所以,先包装,然后反转。
  8. 将字符串设置为text组件,它应该可以工作。通常是这样。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32297691

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档