本文整理汇总了C#中SpriteFont.MeasureString方法的典型用法代码示例。如果您正苦于以下问题:C# SpriteFont.MeasureString方法的具体用法?C# SpriteFont.MeasureString怎么用?C# SpriteFont.MeasureString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SpriteFont
的用法示例。
在下文中一共展示了SpriteFont.MeasureString方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: LoadContent
protected override Task LoadContent()
{
// Load the fonts
spriteFont11 = Asset.Load<SpriteFont>("Arial");
// load the round texture
roundTexture = Asset.Load<Texture>("round");
// create the SpriteBatch used to render them
spriteBatch = new SpriteBatch(GraphicsDevice);
// initialize parameters
textHeight = spriteFont11.MeasureString(KeyboardSessionString).Y;
screenSize = new Vector2(GraphicsDevice.BackBuffer.Width, GraphicsDevice.BackBuffer.Height);
roundTextureSize = new Vector2(roundTexture.Width, roundTexture.Height);
// activate the gesture recognitions
Input.ActivatedGestures.Add(new GestureConfigDrag());
Input.ActivatedGestures.Add(new GestureConfigFlick());
Input.ActivatedGestures.Add(new GestureConfigLongPress());
Input.ActivatedGestures.Add(new GestureConfigComposite());
Input.ActivatedGestures.Add(new GestureConfigTap());
// add a task to the task scheduler that will be executed asynchronously
Script.AddTask(UpdateInputStates);
return Task.FromResult(0);
}
示例2: WordwrapString
public static string[] WordwrapString(string inputString, SpriteFont font, int maxWidth, int maxLines, out int lines)
{
string[] array = new string[maxLines];
int num = 0;
inputString += ' ';
if (!Utils.charLengths.ContainsKey(font))
{
float[] array2 = new float[256];
for (int i = 0; i < 256; i++)
{
array2[i] = font.MeasureString(string.Concat((char)i)).X;
}
Utils.charLengths.Add(font, array2);
}
byte[] bytes = Encoding.ASCII.GetBytes(inputString);
StringBuilder stringBuilder = new StringBuilder();
StringBuilder stringBuilder2 = new StringBuilder();
float num2 = 0f;
bool flag = true;
for (int j = 0; j < bytes.Length; j++)
{
if (bytes[j] == 10)
{
num++;
if (num == maxLines)
{
break;
}
stringBuilder.Append(stringBuilder2);
stringBuilder2.Clear();
stringBuilder.Append('\n');
num2 = 0f;
flag = true;
}
else
{
float num3 = Utils.charLengths[font][(int)bytes[j]];
if (num2 + num3 >= (float)(maxWidth - 20))
{
if (!flag)
{
num++;
if (num == maxLines)
{
break;
}
stringBuilder.Append('\n');
stringBuilder.Append(stringBuilder2);
stringBuilder.Append((char)bytes[j]);
stringBuilder2.Clear();
num2 = 0f;
flag = true;
}
else
{
stringBuilder.Append(stringBuilder2);
stringBuilder.Append('-');
num++;
if (num == maxLines)
{
break;
}
stringBuilder2.Clear();
stringBuilder2.Append('\n');
stringBuilder2.Append((char)bytes[j]);
num2 = num3;
flag = true;
}
}
else
{
stringBuilder2.Append((char)bytes[j]);
if (bytes[j] == 32)
{
stringBuilder.Append(stringBuilder2);
stringBuilder2.Clear();
flag = false;
}
num2 += num3;
}
}
}
stringBuilder.Remove(stringBuilder.Length - 1, 1);
array = stringBuilder.ToString().Split(new char[]
{
'\n'
});
int k = array.Length;
Array.Resize<string>(ref array, maxLines);
while (k < maxLines)
{
array[k++] = "";
}
lines = Math.Min(num, maxLines - 1);
return array;
}
示例3: WordwrapString
public static string[] WordwrapString(string text, SpriteFont font, int maxWidth, int maxLines, out int lineAmount)
{
string[] array = new string[maxLines];
int num = 0;
List<string> list = new List<string>(text.Split(new char[]
{
'\n'
}));
List<string> list2 = new List<string>(list[0].Split(new char[]
{
' '
}));
for (int i = 1; i < list.Count; i++)
{
list2.Add("\n");
list2.AddRange(list[i].Split(new char[]
{
' '
}));
}
bool flag = true;
while (list2.Count > 0)
{
string text2 = list2[0];
string str = " ";
if (list2.Count == 1)
{
str = "";
}
if (text2 == "\n")
{
string[] array2;
IntPtr intPtr;
(array2 = array)[(int)(intPtr = (IntPtr)(num++))] = array2[(int)intPtr] + text2;
if (num >= maxLines)
{
break;
}
list2.RemoveAt(0);
}
else if (flag)
{
if (font.MeasureString(text2).X > (float)maxWidth)
{
string text3 = string.Concat(text2[0]);
int num2 = 1;
while (font.MeasureString(text3 + text2[num2] + '-').X <= (float)maxWidth)
{
text3 += text2[num2++];
}
text3 += '-';
array[num++] = text3 + " ";
if (num >= maxLines)
{
break;
}
list2.RemoveAt(0);
list2.Insert(0, text2.Substring(num2));
}
else
{
string[] array3;
IntPtr intPtr2;
(array3 = array)[(int)(intPtr2 = (IntPtr)num)] = array3[(int)intPtr2] + text2 + str;
flag = false;
list2.RemoveAt(0);
}
}
else if (font.MeasureString(array[num] + text2).X > (float)maxWidth)
{
num++;
if (num >= maxLines)
{
break;
}
flag = true;
}
else
{
string[] array4;
IntPtr intPtr3;
(array4 = array)[(int)(intPtr3 = (IntPtr)num)] = array4[(int)intPtr3] + text2 + str;
flag = false;
list2.RemoveAt(0);
}
}
lineAmount = num;
if (lineAmount == maxLines)
{
lineAmount--;
}
return array;
}
示例4: ConstrainStringSolsund
public static string[] ConstrainStringSolsund(string inputString, SpriteFont font, int maxWidth, int maxLines, out int lines)
{
string[] array = new string[maxLines];
int num = 0;
int num2 = 0;
int num3 = 0;
for (int i = 0; i < inputString.Length; i++)
{
byte[] bytes = Encoding.ASCII.GetBytes(inputString.Substring(i, 1));
if (bytes[0] == 10)
{
if (font.MeasureString(inputString.Substring(num2, i - num2)).X > (float)maxWidth)
{
array[num] = inputString.Substring(num2, num3 - num2);
num++;
num2 = num3 + 1;
}
array[num] = inputString.Substring(num2, i - num2);
num++;
num2 = i + 1;
num3 = i + 1;
}
else
{
if (inputString.Substring(i, 1) == " " || i == inputString.Length - 1)
{
if (font.MeasureString(inputString.Substring(num2, i - num2)).X > (float)maxWidth)
{
array[num] = inputString.Substring(num2, num3 - num2);
num++;
num2 = num3 + 1;
}
num3 = i;
}
}
if (num == maxLines)
{
inputString = inputString.Substring(0, i - 1);
num2 = i - 1;
num = maxLines - 1;
break;
}
}
if (num < maxLines)
{
array[num] = inputString.Substring(num2, inputString.Length - num2);
}
lines = num;
return array;
}