static float GetLineWidth(string subString, GUIBase_FontBase inFont) { float lineWidth = 0; GUIBase_FontEx fontEx = inFont as GUIBase_FontEx; GUIBase_Font font = inFont as GUIBase_Font; if (fontEx != null) { for (int i = 0; i < subString.Length; ++i) { lineWidth += fontEx.GetCharWidth((int)subString[i]); } } else { for (int i = 0; i < subString.Length; ++i) { lineWidth += font.GetCharWidth((int)subString[i]); } } return(lineWidth); }
public static List <TextLine> GetLines(string inText, GUIBase_FontEx inFont, HorizontalTextAlignment inAlignment, float inMaxlineWidth, Vector3 inScale, float inLineSpacePct, bool isForTextField = false) { if (string.IsNullOrEmpty(inText) == true) { return(null); } if (inMaxlineWidth <= 0.0f) { return(null); } if (inScale == Vector3.zero) { return(null); } List <TextLine> lines = new List <TextLine>(); TextLine newLine = null; int lastSpaceIndex = -1; float widthAtLastSpace = 0; float widthOfSpace = inFont.GetCharWidth((int)' ') * inScale.x; float charWidth = 0; float fontHeight = inFont.GetFontHeight(); for (int i = 0; i < inText.Length; ++i) { if (newLine == null) { newLine = new TextLine(); newLine.m_StartIndex = i; newLine.m_SpaceWidth = widthOfSpace; lastSpaceIndex = -1; widthAtLastSpace = 0; } int character = (int)inText[i]; if (!isForTextField && character == ' ') { lastSpaceIndex = i; widthAtLastSpace = newLine.m_Size.x; newLine.m_Size.x += widthOfSpace; newLine.m_NumOfSpaces++; continue; } switch (character) { case '\n': newLine.m_EndIndex = i; newLine.m_EndOfParagraph = true; lines.Add(newLine); newLine = null; break; default: charWidth = inFont.GetCharWidth(character) * inScale.x; if (newLine.m_Size.x + charWidth > inMaxlineWidth) { if (lastSpaceIndex >= 0) { newLine.m_NumOfSpaces--; newLine.m_Size.x = widthAtLastSpace; newLine.m_EndIndex = lastSpaceIndex; i = lastSpaceIndex; } else { newLine.m_EndIndex = i; i = i - 1; } newLine.m_EndOfParagraph = false; if ((newLine.m_EndIndex - newLine.m_StartIndex) < 1) { //this line is invalid. It looks that actual character is too width for inMaxlineWidth // skip it. Debug.LogWarning("Can't generate line for character: " + (char)character); } else { lines.Add(newLine); } newLine = null; } else { newLine.m_Size.x += charWidth; } break; } } if (newLine != null) { newLine.m_EndIndex = inText.Length; newLine.m_EndOfParagraph = true; if ((newLine.m_EndIndex - newLine.m_StartIndex) == 0) { Debug.LogWarning("Empty line"); } else { lines.Add(newLine); } } float yOffset = 0; // Compute x offset of lines by TextAlignment. foreach (TextLine line in lines) { #if !MADFINGER_KEYBOARD_MOUSE // remove spaces from start and end of line... TrimSpaces(inText, line, widthOfSpace); #endif // Check line validity... if ((line.m_EndIndex - line.m_StartIndex) == 0) { //Debug.LogWarning("Empty line"); continue; } // setup line x offset based on TextAlignment... float span = (inMaxlineWidth - line.m_Size.x); switch (inAlignment) { case HorizontalTextAlignment.Left: line.m_Offset.x = 0.0f; break; case HorizontalTextAlignment.Center: line.m_Offset.x = span * 0.5f; break; case HorizontalTextAlignment.Right: line.m_Offset.x = span; break; case HorizontalTextAlignment.Justify: if (line.m_EndOfParagraph == false && line.m_NumOfSpaces > 0) { line.m_SpaceWidth += span / (float)line.m_NumOfSpaces; } break; default: Debug.LogError("Unknown Horizontal text alignment !!!! " + inAlignment); break; } yOffset += 0.5f * fontHeight * inScale.y; line.m_Offset.y = yOffset; yOffset += 0.5f * fontHeight * inScale.y; yOffset += inLineSpacePct * fontHeight * inScale.y; } return(lines); }