您的位置:首页 > 移动开发 > Unity3D

unity3d NGUI满足不了需求,修改源代码记录

2015-10-15 18:54 281 查看
问题:UILabel一行中有空格并且一行满了,在空格后面会强制换行

最终效果:空格后面不换行

修改处:NGUIText.cs    849-860 883-889 892 898  屏蔽代码

版本:3.5



static public bool WrapText (string text, out string finalText, bool keepCharCount)
{
if (rectWidth < 1 || rectHeight < 1 || finalLineHeight < 1f)
{
finalText = "";
return false;
}

float height = (maxLines > 0) ? Mathf.Min(rectHeight, finalLineHeight * maxLines) : rectHeight;
int maxLineCount = (maxLines > 0) ? maxLines : 1000000;
maxLineCount = Mathf.FloorToInt(Mathf.Min(maxLineCount, height / finalLineHeight) + 0.01f);

if (maxLineCount == 0)
{
finalText = "";
return false;
}

if (string.IsNullOrEmpty(text)) text = " ";
Prepare(text);

StringBuilder sb = new StringBuilder();
int textLength = text.Length;
float remainingWidth = rectWidth;
int start = 0, offset = 0, lineCount = 1, prev = 0;
bool lineIsEmpty = true;

// Run through all characters
for (; offset < textLength; ++offset)
{
char ch = text[offset];

// New line character -- start a new line
if (ch == '\n')
{
if (lineCount == maxLineCount) break;
remainingWidth = rectWidth;

// Add the previous word to the final string
if (start < offset) sb.Append(text.Substring(start, offset - start + 1));
else sb.Append(ch);

lineIsEmpty = true;
++lineCount;
start = offset + 1;
prev = 0;
continue;
}

// When encoded symbols such as [RrGgBb] or [-] are encountered, skip past them
if (encoding && ParseSymbol(text, ref offset)) { --offset; continue; }

// See if there is a symbol matching this text
BMSymbol symbol = useSymbols ? GetSymbol(text, offset, textLength) : null;

// Calculate how wide this symbol or character is going to be
float glyphWidth;

if (symbol == null)
{
// Find the glyph for this character
float w = GetGlyphWidth(ch, prev);
if (w == 0f) continue;
glyphWidth = finalSpacingX + w;
}
else glyphWidth = finalSpacingX + symbol.advance * fontScale;

// Reduce the width
remainingWidth -= glyphWidth;

// If this marks the end of a word, add it to the final string.
//屏蔽
// if (ch == ' ' && prev != ' ' && start < offset)
// {
// int end = offset - start + 1;
//
// // Last word on the last line should not include an invisible character
// if (lineCount == maxLineCount && remainingWidth <= 0f && offset < textLength && text[offset] <= ' ') --end;
//
// sb.Append(text.Substring(start, end));
// lineIsEmpty = false;
// start = offset + 1;
// prev = ch;
// }

// Doesn't fit?
if (remainingWidth < 0f)
{
// Can't start a new line
if (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)));

if (lineCount++ == maxLineCount)
{
start = offset;
break;
}

if (keepCharCount) ReplaceSpaceWithNewline(ref sb);
else EndLine(ref sb);

// Start a brand-new line
lineIsEmpty = true;
//屏蔽
// if (ch == ' ')
// {
// start = offset + 1;
// remainingWidth = rectWidth;
// }
// else
// {
start = offset;
remainingWidth = rectWidth - glyphWidth;
//屏蔽
// }
prev = 0;
}
else
{
// Skip all spaces before the word
//屏蔽
// while (start < textLength && text[start] == ' ') ++start;

// Revert the position to the beginning of the word and reset the line
lineIsEmpty = true;
remainingWidth = rectWidth;
offset = start - 1;
prev = 0;

if (lineCount++ == maxLineCount) break;
if (keepCharCount) ReplaceSpaceWithNewline(ref sb);
else EndLine(ref sb);
continue;
}
}
else prev = ch;

// Advance the offset past the symbol
if (symbol != null)
{
offset += symbol.length - 1;
prev = 0;
}
}

if (start < offset) sb.Append(text.Substring(start, offset - start));
finalText = sb.ToString();
return (offset == textLength) || (lineCount <= Mathf.Min(maxLines, maxLineCount));
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: