解决Unity中Text控件行首出现标点符号问题

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.UI;
 
public class TextFit : Text
{
    /// <summary>
    /// 用于匹配标点符号(正则表达式)
    /// </summary>
    private readonly string strRegex = @"\p{P}";
 
    /// <summary>
    /// 用于存储text组件中的内容
    /// </summary>
    private System.Text.StringBuilder MExplainText = null;
 
    /// <summary>
    /// 用于存储text生成器中的内容
    /// </summary>
    private IList<UILineInfo> MExpalinTextLine;
    protected override void OnPopulateMesh(VertexHelper toFill)
    {
        base.OnPopulateMesh(toFill);
        StartCoroutine(MClearUpExplainMode(this,text));
    }
    /// <summary>
    /// 整理文字。确保首字母不出现标点
    /// </summary>
    /// <param name="_component">text组件</param>
    /// <param name="_text">需要填入text中的内容</param>
    /// <returns></returns>
    IEnumerator MClearUpExplainMode(Text _component, string _text)
    {
        _component.text = _text;
 
        //如果直接执行下边方法的话,那么_component.cachedTextGenerator.lines将会获取的是之前text中的内容,而不是_text的内容,所以需要等待一下
        yield return new WaitForSeconds(0.001f);
 
 
        MExpalinTextLine = _component.cachedTextGenerator.lines;
 
        //需要改变的字符序号
        int mChangeIndex = -1;
 
        MExplainText = new System.Text.StringBuilder(_component.text);
 
        for (int i = 1; i < MExpalinTextLine.Count; i++)
        {
            try
            {
                //首位是否有标点
                bool _b = Regex.IsMatch(_component.text[MExpalinTextLine[i].startCharIdx].ToString(), strRegex);
 
                if (_b)
                {
 
                    mChangeIndex = MExpalinTextLine[i].startCharIdx - 1;
 
                    MExplainText.Insert(mChangeIndex, "\n");
                }
                
            }
            catch (Exception e)
            {
                Debug.Log("出错了");
            }
            
        }
 
        _component.text = MExplainText.ToString();
 
        //_component.text = _text;
 
    }
}

以下为优化改进版本

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.UI;
 
public class TextFit : Text
{
    /// <summary>
    /// 用于匹配标点符号(正则表达式)
    /// </summary>
    private readonly string strRegex = @"\p{P}";
 
    /// <summary>
    /// 用于存储text组件中的内容
    /// </summary>
    private System.Text.StringBuilder MExplainText = null;
 
    /// <summary>
    /// 用于存储text生成器中的内容
    /// </summary>
    private IList<UILineInfo> MExpalinTextLine;
    protected override void OnPopulateMesh(VertexHelper toFill)
    {
        base.OnPopulateMesh(toFill);
        StartCoroutine(MClearUpExplainMode(this,text));
    }
    /// <summary>
    /// 整理文字。确保首字母不出现标点
    /// </summary>
    /// <param name="_component">text组件</param>
    /// <param name="_text">需要填入text中的内容</param>
    /// <returns></returns>
    IEnumerator MClearUpExplainMode(Text _component, string _text)
    {
        _component.text = _text;
 
        //如果直接执行下边方法的话,那么_component.cachedTextGenerator.lines将会获取的是之前text中的内容,而不是_text的内容,所以需要等待一下
        yield return new WaitForSeconds(0.001f);
 
 
        MExpalinTextLine = _component.cachedTextGenerator.lines;
 
        //需要改变的字符序号
        int mChangeIndex = -1;
 
        MExplainText = new System.Text.StringBuilder(_component.text);
        // 从第二行开始进行检测
        for (int i = 1; i < MExpalinTextLine.Count;i++ )
        {
            try
            {
                if (MExpalinTextLine[i].startCharIdx >= _component.text.Length) continue;
                //首位是否有标点
                bool _b = Regex.IsMatch(MExplainText.ToString()[MExpalinTextLine[i].startCharIdx].ToString(), strRegex);
 
                if (_b)
                {
                    
                    
                    mChangeIndex = MExpalinTextLine[i].startCharIdx - 1;
                    // 解决联系多个都是标点符号的问题
                    for (int j = MExpalinTextLine[i].startCharIdx - 1; j > 0; j--)
                    {
                        bool _c = Regex.IsMatch(MExplainText.ToString()[j].ToString(), strRegex);
                        if (_c)
                        {
                            mChangeIndex--;
                        }
                        else
                        {
                            break;
                        }
                    }
                    
                    
                
                    MExplainText.Insert(mChangeIndex, "\n");
                    // MExplainText = new System.Text.StringBuilder(_component.text);
                }
                
                
            }
            catch (Exception e)
            {
                Debug.Log("出错了"+e.Message);
            }
            
        }

        _component.text = MExplainText.ToString();

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值