Unity Text行首不出现中文标点

19 篇文章 0 订阅

更新:还是使用Unity的TexMesh Pro 比较好,运行快,自动排除首行出现标点的可能。富文本还好用。

private readonly string markList = "(\!|\?|\,|\。|\《|\》|\)|\:|\“|\‘|\、|\;|\+|\-)";
StringBuilder textStr;

public override void SetVerticesDirty()
    {
        var settings = GetGenerationSettings(rectTransform.rect.size);
        cachedTextGenerator.Populate(this.text, settings);

        textStr = new StringBuilder(this.text);

        IList<UILineInfo> lineList = this.cachedTextGenerator.lines;
        int changeIndex = -1;
        for (int i = 1; i < lineList.Count; i++)
        {
            bool isMark = Regex.IsMatch(text[lineList[i].startCharIdx].ToString(), markList);

            if (isMark)
            {
                changeIndex = lineList[i].startCharIdx - 1;
                string str = text.Substring(lineList[i - 1].startCharIdx, lineList[i].startCharIdx);
                MatchCollection richStrMatch = Regex.Matches(str, ".(</color>|<color=#\\w{6}>|" + markList + ")+$");
                if (richStrMatch.Count > 0)
                {
                    string richStr = richStrMatch[0].ToString();
                    int length = richStr.Length;
                    changeIndex = lineList[i].startCharIdx - length;
                    break;
                }
            }
        }

        if (changeIndex >= 0)
        {
            textStr.Insert(changeIndex, '\n');
            this.text = textStr.ToString();
        }

        base.SetVerticesDirty();
    }

如果文字过多,这个代码执行一次后,会影响其他文字,下面的还是可能会出现标点符号出现在首位。。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值