本文整理汇总了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]);
}
}
示例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);
}
}