当前位置: 首页>>代码示例>>C#>>正文


C# SpriteFont.MeasureCharacter方法代码示例

本文整理汇总了C#中Microsoft.Xna.Framework.Graphics.SpriteFont.MeasureCharacter方法的典型用法代码示例。如果您正苦于以下问题:C# SpriteFont.MeasureCharacter方法的具体用法?C# SpriteFont.MeasureCharacter怎么用?C# SpriteFont.MeasureCharacter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Microsoft.Xna.Framework.Graphics.SpriteFont的用法示例。


在下文中一共展示了SpriteFont.MeasureCharacter方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: WordWrap

 public static void WordWrap(this StringBuilder stringBuilder, ref StringBuilder target, SpriteFont font, Vector2 bounds, Vector2 scale)
 {
     int lastWhiteSpaceIndex = 0;
     float currentLineWidth = 0;
     float lengthSinceLastWhiteSpace = 0;
     Vector2 characterSize = Vector2.Zero;
     int lines = 0;
     for (int i = 0; i < stringBuilder.Length; i++)
     {
         characterSize = font.MeasureCharacter(stringBuilder[i]) * scale;
         currentLineWidth += characterSize.X;
         lengthSinceLastWhiteSpace += characterSize.X;
         if ((stringBuilder[i] != '\r') && (stringBuilder[i] != '\n'))
         {
             if (currentLineWidth > bounds.X)
             {
                 if ((lines + 1) * font.LineSpacing > bounds.Y)
                     return;
                 lines++;
                 if (char.IsWhiteSpace(stringBuilder[i]))
                 {
                     target.Insert(i, _newLineChars);
                     currentLineWidth = 0;
                     lengthSinceLastWhiteSpace = 0;
                     continue;
                 }
                 else
                 {
                     target.Insert(lastWhiteSpaceIndex, _newLineChars);
                     target.Remove(lastWhiteSpaceIndex + _newLineChars.Length, 1);
                     currentLineWidth = lengthSinceLastWhiteSpace;
                     lengthSinceLastWhiteSpace = 0;
                 }
             }
             else
             {
                 if (char.IsWhiteSpace(stringBuilder[i]))
                 {
                     lastWhiteSpaceIndex = target.Length;
                     lengthSinceLastWhiteSpace = 0;
                 }
             }
         }
         else
         {
             lengthSinceLastWhiteSpace = 0;
             currentLineWidth = 0;
         }
         target.Append(stringBuilder[i]);
     }
 }
开发者ID:Raidenthequick,项目名称:delta,代码行数:51,代码来源:StringBuilderExtensions.cs

示例2: WrapWord

 public static void WrapWord(StringBuilder original, StringBuilder target, SpriteFont font, Rectangle bounds, float scale)
 {
     int lastWhiteSpace = 0;
     float currentLength = 0;
     float lengthSinceLastWhiteSpace = 0;
     float characterWidth = 0;
     for (int i = 0; i < original.Length; i++)
     {
         //get the character
         char character = original[i];
         //measure the length of the current line
         characterWidth = font.MeasureCharacter(character).X * scale;
         currentLength += characterWidth;
         //find the length since last white space
         lengthSinceLastWhiteSpace += characterWidth;
         //are we at a new line?
         if ((character != '\r') && (character != '\n'))
         {
             //time for a new line?
             if (currentLength > bounds.Width)
             {
                 //if so are we at white space?
                 if (char.IsWhiteSpace(character))
                 {
                     //if so insert newline here
                     target.Insert(i, NewLine);
                     //reset lengths
                     currentLength = 0;
                     lengthSinceLastWhiteSpace = 0;
                     // return to the top of the loop as to not append white space
                     continue;
                 }
                 else
                 {
                     //not at white space so we insert a new line at the previous recorded white space
                     target.Insert(lastWhiteSpace, NewLine);
                     //remove the white space
                     target.Remove(lastWhiteSpace + NewLine.Length, 1);
                     //make sure the the characters at the line break are accounted for
                     currentLength = lengthSinceLastWhiteSpace;
                     lengthSinceLastWhiteSpace = 0;
                 }
             }
             else
             {
                 //not time for a line break? are we at white space?
                 if (char.IsWhiteSpace(character))
                 {
                     //record it's location
                     lastWhiteSpace = target.Length;
                     lengthSinceLastWhiteSpace = 0;
                 }
             }
         }
         else
         {
             lengthSinceLastWhiteSpace = 0;
             currentLength = 0;
         }
         //always append
         target.Append(character);
     }
 }
开发者ID:Zikomo,项目名称:Leximo,代码行数:63,代码来源:WordWrapper.cs


注:本文中的Microsoft.Xna.Framework.Graphics.SpriteFont.MeasureCharacter方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。