Nez.Portable/PipelineRuntime/BitmapFonts/BitmapFont.cs
/// <summary>
/// truncates text and returns a new string with ellipsis appended if necessary. This method ignores all
/// line breaks.
/// </summary>
/// <returns>The text.</returns>
/// <param name="text">Text.</param>
/// <param name="ellipsis">Ellipsis.</param>
/// <param name="maxLineWidth">Max line width.</param>
truncates text and returns a new string with ellipsis appended if necessary. This method ignores all
line breaks.
truncates text and returns a new string with ellipsis appended if necessary . This method ignores all line breaks .
public string truncateText(string text, string ellipsis, float maxLineWidth)
        {
            if (maxLineWidth < spaceWidth)
                return string.Empty;
            var size = measureString(text);
            var ellipsisWidth = measureString(ellipsis).X;
            if (size.X > maxLineWidth)
            {
                var sb = new StringBuilder();
                var width = 0.0f;
                BitmapFontRegion currentFontRegion = null;
                var offsetX = 0.0f;
                for (var i = 0; i < text.Length; i++)
                {
                    var c = text[i];
                    if (c == '\r' || c == '\n')
                        continue;
                    if (currentFontRegion != null)
                        offsetX += spacing + currentFontRegion.xAdvance;
                    if (!_characterMap.TryGetValue(c, out currentFontRegion))
                        currentFontRegion = defaultCharacterRegion;
                    var proposedWidth = offsetX + currentFontRegion.xAdvance + spacing;
                    if (proposedWidth > width)
                        width = proposedWidth;
                    if (width < maxLineWidth - ellipsisWidth)
                    {
                        sb.Append(c);
                    }
                    else
                    {
                        sb.Append(ellipsis);
                        break;
                    }
                }
                return sb.ToString();
            }
            return text;
        }
public string truncateText ( string text , string ellipsis , float maxLineWidth ) { if ( maxLineWidth < spaceWidth ) return string . Empty ; var size = measureString ( text ) ; var ellipsisWidth = measureString ( ellipsis ) . X ; if ( size . X > maxLineWidth ) { var sb = new StringBuilder ( ) ; var width = 0.0f ; BitmapFontRegion currentFontRegion = null ; var offsetX = 0.0f ; for ( var i = 0 ; i < text . Length ; i ++ ) { var c = text [ i ] ; if ( c == ' \r ' || c == ' \n ' ) continue ; if ( currentFontRegion != null ) offsetX += spacing + currentFontRegion . xAdvance ; if ( ! _characterMap . TryGetValue ( c , out currentFontRegion ) ) currentFontRegion = defaultCharacterRegion ; var proposedWidth = offsetX + currentFontRegion . xAdvance + spacing ; if ( proposedWidth > width ) width = proposedWidth ; if ( width < maxLineWidth - ellipsisWidth ) { sb . Append ( c ) ; } else { sb . Append ( ellipsis ) ; break ; } } return sb . ToString ( ) ; } return text ; }
truncates text and returns a new string with ellipsis appended if necessary.
truncates text and returns a new string with ellipsis appended if necessary .
// do we even need to truncate? // determine how many chars we can fit in maxLineWidth - ellipsisWidth // we dont deal with line breaks or tabs // no more room. append our ellipsis and get out of here
None
text
string
None
ellipsis
string
None
maxLineWidth
float
Max line width.
Max line width .