首页 > 代码库 > NGUI 解决UILable 在空行起始位置加‘\n’

NGUI 解决UILable 在空行起始位置加‘\n’

NGUI 解决UILable 默认在顶满第一行时,在起始位置如键入空格无效,其原因就是会加入换行符,使字符串,整体换行了

 

解决办法加入bool变量控制

1在 UILable代码中添加

[HideInInspector][SerializeField] public bool wrapText = true;

 2 在该函数中 void ProcessText (bool legacyMode, bool full)

对方法

 // Wrap the text     

bool fits = NGUIText.WrapText(mText, out mProcessedText, true, wrapText);添加参数

 

3 在NGUIText源码中相应函数添加参数

static public bool WrapText (string text, out string finalText)    {        return WrapText(text, out finalText, false);    }
static public bool WrapText (string text, out string finalText, bool keepCharCount, bool needWrap = true)

 

// Doesn‘t fit?            if (Mathf.RoundToInt(remainingWidth) < 0)            {                // Can‘t start a new line 在此处添加判断 !needWrap 来阻止创建新行,就解决问题了                     if ( !needWrap || lineIsEmpty || lineCount == maxLineCount)                {                    // This is the first word on the line -- add it up to the character that fits                    sb.Append(text.Substring(start, Mathf.Max(0, offset - start)));                    bool space = IsSpace(ch);                    if (!space && !eastern) fits = false;                    if (lineCount++ == maxLineCount)                    {                        start = offset;                        break;                    }                    if (keepCharCount) ReplaceSpaceWithNewline(ref sb);                    else EndLine(ref sb);                    // Start a brand-new line                    lineIsEmpty = true;                    if (space)                    {                        start = offset + 1;                        remainingWidth = regionWidth;                    }                    else                    {                        start = offset;                        remainingWidth = regionWidth - glyphWidth;                    }                    prev = 0;                }                else                {                    // Revert the position to the beginning of the word and reset the line                    lineIsEmpty = true;                    remainingWidth = regionWidth;                    offset = start - 1;                    prev = 0;                    if (lineCount++ == maxLineCount) break;                    if (keepCharCount) ReplaceSpaceWithNewline(ref sb);                    else EndLine(ref sb);                    continue;                }

 

NGUI 解决UILable 在空行起始位置加‘\n’