本文整理汇总了C#中Microsoft.Xna.Framework.Graphics.SpriteFont.MeasureString方法的典型用法代码示例。如果您正苦于以下问题:C# SpriteFont.MeasureString方法的具体用法?C# SpriteFont.MeasureString怎么用?C# SpriteFont.MeasureString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Xna.Framework.Graphics.SpriteFont
的用法示例。
在下文中一共展示了SpriteFont.MeasureString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BakeTextCentered
public void BakeTextCentered(SpriteFont font, string text, Vector2 location, Color textColor)
{
var shifted = new Vector2 {
X = (float)Math.Round(location.X - font.MeasureString(text).X / 2),
Y = (float)Math.Round(location.Y - font.MeasureString(text).Y / 2)
};
DrawString(font, text, shifted, textColor);
}
示例2: JoinNetworkGameScreen
public JoinNetworkGameScreen(Game game, SpriteBatch spriteBatch, SpriteFont spriteFont, Texture2D background)
: base(game, spriteBatch)
{
this.background = background;
formBackground = game.Content.Load<Texture2D>("alienmetal");
Vector2 formSize = new Vector2(350, 350);
Vector2 center = new Vector2((this.windowSize.Width - formSize.X) / 2, (this.windowSize.Height - formSize.Y) / 2);
connectionMethodForm = new Form("Connect", "Connection Metod", new Rectangle((int)center.X, (int)center.Y,(int) formSize.X, (int)formSize.Y), formBackground, spriteFont, Color.White);
buttonTexture = game.Content.Load<Texture2D>("buttonTexture");
textboxTexture = game.Content.Load<Texture2D>("textboxTexture");
//figure out the width and heigh of the text on the buttons
Vector2 lanButtonSize, connectButtonSize;
lanButtonSize = spriteFont.MeasureString("Scan Lan");
connectButtonSize = spriteFont.MeasureString("Connect");
lanButton = new Button("ScanLan", "Scan LAN", new Rectangle(32,30,(int)lanButtonSize.X + 10, (int)lanButtonSize.Y + 10), buttonTexture, spriteFont, Color.White);
sendButton = new Button("Connect", "Connect", new Rectangle(32,151,(int)connectButtonSize.X + 10, (int)connectButtonSize.Y + 10), buttonTexture, spriteFont, Color.White);
backButton = new Button("BackButton", @"Back", new Rectangle(32, 192, 95, 23), buttonTexture, spriteFont, Color.White);
textBoxIP = new TextBox("Address", "",100,new Rectangle(32,110,232,20), textboxTexture, spriteFont, Color.Black);
textBoxPort = new TextBox("Port", "", 8, new Rectangle(288,110,60,20), textboxTexture, spriteFont, Color.Black);
connectionMethodForm.AddControl(lanButton);
connectionMethodForm.AddControl(sendButton);
connectionMethodForm.AddControl(backButton);
connectionMethodForm.AddControl(textBoxIP);
connectionMethodForm.AddControl(textBoxPort);
lanButton.onClick += new EHandler(ButtonClick);
sendButton.onClick += new EHandler(ButtonClick);
backButton.onClick += new EHandler(ButtonClick);
imageRectangle = new Rectangle(0, 0, Game.Window.ClientBounds.Width, Game.Window.ClientBounds.Height);
}
示例3: Button
public Button(string str, Vector2 SizeandLocation, SpriteFont Font, Color Color)
{
text = str;
font = Font;
detection = new Rectangle((int)SizeandLocation.X - ((int)font.MeasureString(text).X / 2), (int)SizeandLocation.Y - ((int)font.MeasureString(text).Y / 2), (int)font.MeasureString(text).X, (int)font.MeasureString(text).Y);
color = Color;
}
示例4: WordWrap
public string WordWrap(SpriteFont spritefont, string text, float maxWidth)
{
string[] words = text.Split(' ');
StringBuilder stringbuilder = new StringBuilder();
float linewidth = 0.0f;
float spacewidth = spritefont.MeasureString(" ").X;
foreach (var word in words)
{
Vector2 size = spritefont.MeasureString(word);
if (linewidth + size.X < maxWidth)
{
stringbuilder.Append(word + " ");
linewidth += size.X + spacewidth;
}
else
{
stringbuilder.Append("\n" + word + " ");
linewidth = size.X + spacewidth;
}
}
return stringbuilder.ToString();
}
示例5: WrapText
/* word wrapping fr http://www.xnawiki.com/index.php/Basic_Word_Wrapping
* because I can't be arsed with writing this again */
public static string WrapText(
SpriteFont spriteFont,
string text,
float maxLineWidth
)
{
string[] words = text.Split(' ');
StringBuilder sb = new StringBuilder();
float lineWidth = 0f;
float spaceWidth = spriteFont.MeasureString(" ").X;
foreach (string word in words)
{
Vector2 size = spriteFont.MeasureString(word);
if (lineWidth + size.X < maxLineWidth)
{
sb.Append(word + " ");
lineWidth += size.X + spaceWidth;
}
else
{
sb.Append("\n" + " " + word + " ");
lineWidth = size.X + spaceWidth;
}
}
return sb.ToString();
}
示例6: BreakStringAccoringToLineLength
public static string BreakStringAccoringToLineLength(SpriteFont font, string input, float maxLineLength)
{
var words = input.Split(' ');
var lines = new List<string>();
var spaceLen = font.MeasureString(" ").X;
var currentLineLength = 0f;
StringBuilder currentLine = new StringBuilder();
foreach (var word in words)
{
var wordLen = font.MeasureString(word).X;
if (currentLineLength > 0)
{
if ((currentLineLength + spaceLen + wordLen) > maxLineLength)
{
lines.Add(currentLine.ToString());
currentLine.Clear();
currentLine.Append(word);
currentLineLength = wordLen;
}
else
{
currentLine.Append(' ').Append(word);
currentLineLength += wordLen + spaceLen;
}
}
else
{
currentLine.Append(word);
currentLineLength += wordLen;
}
}
lines.Add(currentLine.ToString());
return string.Join("\n", lines);
}
示例7: ItemText
//------------------------------------------------------------------------------
// Function: ItemText
// Author: nholmes
// Summary: item constructor, allows user to specify text, vertical position,
// alignment and font as well as whether the text is selectable
//------------------------------------------------------------------------------
public ItemText(Menu menu, Game game, string text, int y, Alignment alignment, SpriteFont font, Color fontColor, int textOffset, bool selectable)
: base(menu, game, textOffset)
{
// store the text font and font color to use
this.text = text;
this.font = font;
this.fontColor = fontColor;
// store whether this item is selectable
this.selectable = selectable;
// set the vy position of this text item
position.Y = y;
// calculate position based on alignment provided
switch (alignment)
{
case Alignment.left:
position.X = 0;
break;
case Alignment.centre:
position.X = (int)((menu.ItemArea.Width - font.MeasureString(text).X) / 2);
break;
case Alignment.right:
position.X = (int)(menu.ItemArea.Width - font.MeasureString(text).X);
break;
default:
position.X = 0;
break;
}
// store the size of this text item
size = font.MeasureString(text);
}
示例8: GameState
public GameState(GraphicsDeviceManager g, ContentManager c, Viewport v)
: base(g, c, v)
{
me = this;
viewport = v;
ButtonSize = new Vector2(viewport.Width * 0.34f, viewport.Width * 0.17f);
btnPlay = content.Load<Texture2D>("btnPlay");
playLocation = new Vector2((viewport.Width - ButtonSize.X) / 2, viewport.Height * 0.6f);
score = new Score(v, c);
score.display = true;
player = new Player(v);
player.LoadContent(c);
obstacles = new Obstacles(viewport);
obstacles.LoadContent(content);
spriteFont = content.Load<SpriteFont>("ScoreFont");
READEsize = spriteFont.MeasureString("Get Ready");
Menusize = spriteFont.MeasureString("Twerkopter");
GameOverSize = spriteFont.MeasureString("Game Over");
GameOverLocation = new Vector2((viewport.Width - spriteFont.MeasureString("Game Over").X) / 2, -GameOverSize.Y);
Scoreboard = content.Load<Texture2D>("Dashboard");
ScoreboardSize = new Vector2(viewport.Width * .9f, viewport.Width * 0.545625f);
ScoreboardLocation = new Vector2((viewport.Width - ScoreboardSize.X) / 2, viewport.Height);
buttonSound = c.Load<SoundEffect>("swing");
}
示例9: DetailsView
public DetailsView(BohemianArtifact bbshelf, Vector3 position, Vector3 size)
{
bookshelf = bbshelf;
bookshelf.Library.SelectedArtifactChanged += new ArtifactLibrary.SelectedArtifactHandler(library_SelectedArtifactChanged);
bookshelf.Library.LanguageChanged += new ArtifactLibrary.ChangeLanguageHandler(Library_LanguageChanged);
this.position = position;
this.size = size;
font = bookshelf.Content.Load<SpriteFont>("Arial");
titleText = new SelectableText(font, "Details", new Vector3(0.4f, 0, 0), bookshelf.GlobalTextColor, Color.White);
titleText.InverseScale(0.8f, size.X, size.Y);
boundingBox = new BoundingBox(new Vector3(0, 0, 0), new Vector3(1, 1, 0), Color.Black, 0.005f);
roundedBox = new RoundedBoundingBox(new Vector3(0, 0, 0), new Vector3(1, 1, 0), new Color(200, 200, 200), 0.008f, this);
float maxWidth = 0.5f; // as a percentage of the width of the details box
maxImageBox = new SelectableQuad(new Vector2(0, 0), new Vector2(maxWidth, maxWidth * size.X / size.Y), Color.White);
rotationRange = new FloatRange(0, 10, 1, (float)(-Math.PI / 24), (float)(Math.PI / 24));
batch = new SpriteBatch(XNA.GraphicsDevice);
headerScale = 0.9f;
subheaderScale = 0.7f;
bodyScale = 0.55f;
heightOfBodyLine = font.MeasureString("Eg").Y * bodyScale; // Eg = a high capital letter + low-hanging lowercase to fill the space
heightofSpaceBetweenLines = font.MeasureString("Eg\nEg").Y * bodyScale - 2 * heightOfBodyLine;
//setLengths(bbshelf.Library.Artifacts); // for debugging
maxImageBox.TouchReleased += new TouchReleaseEventHandler(maxImageBox_TouchReleased);
//bbshelf.SelectableObjects.AddObject(maxImageBox); // for debugging
}
示例10: wrapText
/// <summary>
/// This function takes a string and a vector2. It splits the string given by spaces
/// and then for each word it will check the length of it against the length of the
/// vector2 given in. When the word is passed the edge a new line is put in.
/// </summary>
/// <param name="font">The font to use for measuments</param>
/// <param name="text">Text to be wrapped</param>
/// <param name="containerDemensions">Dimensions of the container the text is to be wrapped in</param>
/// <returns>The text including newlines to fit into the container</returns>
public static String wrapText(SpriteFont font, String text, Vector2 containerDemensions)
{
String line = String.Empty;
String returnString = String.Empty;
String[] wordArray = text.Split(' ');
foreach (String word in wordArray)
{
if (font.MeasureString(line + word).Length() > containerDemensions.X)
{
returnString += line + '\n';
line = String.Empty;
// If the string is longer than the box, we need to stop
if (font.MeasureString(returnString).Y > containerDemensions.Y)
{
returnString = returnString.Substring(0, returnString.Length - 3) + "...";
break;
}
}
line += word + ' ';
}
return returnString + line;
}
示例11: WrapText
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Wrap text. </summary>
///
/// <remarks> Frost, 16.11.2010. </remarks>
///
/// <param name="font"> The font. </param>
/// <param name="text"> The text. </param>
/// <param name="maximumLineLength"> Length of the maximum line. </param>
///
/// <returns> . </returns>
////////////////////////////////////////////////////////////////////////////////////////////////////
public static string WrapText(SpriteFont font, string text, float maximumLineLength)
{
string[] words = text.Split(' ');
StringBuilder sb = new StringBuilder();
float spaceLength = font.MeasureString(" ").X;
float lineLength = 0;
foreach (string word in words)
{
float wordLength = font.MeasureString(word).X;
if (wordLength + lineLength < maximumLineLength)
{
sb.Append(word + " ");
lineLength += wordLength + spaceLength;
}
else
{
sb.Append("\n" + word + " ");
lineLength = wordLength + spaceLength;
}
}
return sb.ToString();
}
示例12: LoadContent
public override void LoadContent()
{
if (m_content == null)
m_content = new ContentManager(ScreenManager.Game.Services, "Content");
m_menu_fnt = m_content.Load<SpriteFont>("Fonts\\atarifont");
m_selector_txt = m_content.Load<Texture2D>("Pong\\Textures\\selector");
m_selector_snd = m_content.Load<SoundEffect>("Pong\\Sounds\\paddlehit");
m_resume = new MenuFontItem(ScreenManager.GraphicsDevice.Viewport.Width / 2 - (int)m_menu_fnt.MeasureString("RESUME").X/2, 150, "RESUME", m_menu_fnt);
m_restart = new MenuFontItem(ScreenManager.GraphicsDevice.Viewport.Width / 2 - (int)m_menu_fnt.MeasureString("RESTART").X / 2, 155 + m_resume.Height, "RESTART", m_menu_fnt);
m_quit = new MenuFontItem(ScreenManager.GraphicsDevice.Viewport.Width / 2 - (int)m_menu_fnt.MeasureString("QUIT").X / 2, 155 + m_resume.Height + m_restart.Height, "QUIT", m_menu_fnt);
m_selector = new Sprite(m_selector_txt);
m_selector.X_Pos = m_resume.X_Pos - (int)(m_selector.Width*1.5);
m_selector.Y_Pos = m_resume.Y_Pos + m_resume.Height / 2 - m_selector.Height/2;
// Hook up menu event handlers.
m_resume.m_selected_events += OnCancel;
m_restart.m_selected_events += RestartGameMenuEntrySelected;
m_quit.m_selected_events += QuitGameMenuEntrySelected;
// Add entries to the menu.
MenuEntries.Add(m_resume);
MenuEntries.Add(m_restart);
MenuEntries.Add(m_quit);
m_selected = m_resume;
}
示例13: LoadContent
// Loading the content and setting up said content
public void LoadContent(ContentManager Content, GraphicsDevice graphicsDevice)
{
backgroundManager.LoadContent(Content, graphicsDevice, @"Backgrounds\Clouds\Cloud-Blue-1", @"Backgrounds\Clouds\Cloud-Blue-2");
HeaderFont = Content.Load<SpriteFont>(@"Fonts\StartMenuHeaderFont");
HUDFont = Content.Load<SpriteFont>(@"Fonts\HUDFont");
HeaderPosition = new Vector2(Game1.ViewPortWidth / 2 - (HeaderFont.MeasureString("Levi Challenge").X / 2), Game1.ViewPortHeight / 20);
MadeByPosition = new Vector2(10, Game1.ViewPortHeight - HUDFont.MeasureString("Made by Nathan Todd").Y);
boxHeaderLine = new GuiBox(new Vector2(Game1.ViewPortWidth / 2 - (float)((HeaderFont.MeasureString("Levi Challenge").X * 1.3) / 2), Game1.ViewPortHeight / 6), (int)(HeaderFont.MeasureString("Levi Challenge").X * 1.3), 2, 0, Color.White * 0.4f, Color.White, graphicsDevice);
btnNewGame = new GuiButton(new Vector2(Game1.ViewPortWidth / 2 - 168, Game1.ViewPortHeight / 4), 336, 69, 0, Color.Black * 0.3f, Color.YellowGreen * 0.3f, Color.White, Color.White * 0.6f, "New Game", @"Fonts\StartMenuButtonFont");
btnContinue = new GuiButton(new Vector2(Game1.ViewPortWidth / 2 - 168, Game1.ViewPortHeight / 4 + 69), 336, 69, 0, Color.Black * 0.15f, Color.YellowGreen * 0.15f, Color.White, Color.White * 0.6f, "Continue", @"Fonts\StartMenuButtonFont");
btnOptions = new GuiButton(new Vector2(Game1.ViewPortWidth / 2 - 168, Game1.ViewPortHeight / 4 + 138), 336, 69, 0, Color.Black * 0.3f, Color.YellowGreen * 0.3f, Color.White, Color.White * 0.6f, "Options", @"Fonts\StartMenuButtonFont");
btnExit = new GuiButton(new Vector2(Game1.ViewPortWidth / 2 - 168, Game1.ViewPortHeight / 4 + 207), 336, 69, 0, Color.Black * 0.15f, Color.YellowGreen * 0.15f, Color.White, Color.White * 0.6f, "Exit", @"Fonts\StartMenuButtonFont");
guiSystem.Add(boxHeaderLine);
guiSystem.Add(btnNewGame);
guiSystem.Add(btnContinue);
guiSystem.Add(btnOptions);
guiSystem.Add(btnExit);
guiSystem.LoadContent(Content, graphicsDevice);
guiSystem.ButtonIndexUpdate(0);
}
示例14: DrawErrorMessage
private void DrawErrorMessage(SpriteBatch sb, SpriteFont font, String text)
{
int width = (int)font.MeasureString(text).X, height = (int)font.MeasureString(text).Y;
Rectangle r = new Rectangle((game.ScreenWidth - width) / 2, (game.ScreenHeight - height) / 2, width, height);
Utils.DrawRectangle(sb, game.GraphicsDevice, r, Color.Red);
game.DrawStringAtCenter(sb, text, Color.White);
}
示例15: Menu
public Menu(SpriteFont TitleFont, SpriteFont menuFont)
{
MenuStrings = new List<string>();
SuitStrings = new List<string>();
MenuScreens = new Dictionary<string, Texture2D>();
// add the menu strings
MenuStrings.Add("Start");
MenuStrings.Add("Instructions");
MenuStrings.Add("About");
MenuStrings.Add("Exit");
SuitStrings.Add("1. Hearts");
SuitStrings.Add("2. Diamonds");
SuitStrings.Add("3. Clubs");
SuitStrings.Add("4. Spades");
// add 4 items for now
MenuStringRectangles = new List<Rectangle>();
Vector2 FontOrigin;
string output = "Start";
FontOrigin = TitleFont.MeasureString(output);
int optionAnimationCounter = 3;
float posX = BOARD_WIDTH / 2 - FontOrigin.X / 4 - optionAnimationCounter * 2;
float posY = 216 - FontOrigin.Y / 4;
FontOrigin = TitleFont.MeasureString(output);
MenuStringRectangles.Add(new Rectangle((int)posX, (int)posY, (int)FontOrigin.X, (int)FontOrigin.Y / 2));
output = "Instructions";
FontOrigin = TitleFont.MeasureString(output);
posY = 344 - FontOrigin.Y / 4;
posY = 344 - FontOrigin.Y / 4;
posX -= optionAnimationCounter;
MenuStringRectangles.Add(new Rectangle((int)posX, (int)posY, (int)FontOrigin.X, (int)FontOrigin.Y / 2));
output = "About";
FontOrigin = TitleFont.MeasureString(output);
posY = 471 - FontOrigin.Y / 4;
MenuStringRectangles.Add(new Rectangle((int)posX, (int)posY, (int)FontOrigin.X, (int)FontOrigin.Y / 2));
output = "Exit";
FontOrigin = TitleFont.MeasureString(output);
posY = 608 - FontOrigin.Y / 4;
posX -= optionAnimationCounter * 10;
MenuStringRectangles.Add(new Rectangle((int)posX, (int)posY, (int)FontOrigin.X + optionAnimationCounter * 10, (int)FontOrigin.Y / 2));
int startY = 170;
int startX = 20;
int increment = 75;
TrumpStringRectangles = new List<Rectangle>();
for (int index = 0; index < SuitStrings.Count; index++)
{
output = SuitStrings[index];
FontOrigin = menuFont.MeasureString(output);
startY += increment;
TrumpStringRectangles.Add(new Rectangle(startX, startY, (int)(FontOrigin.X * 0.7), (int)(FontOrigin.Y * 0.7)));
}
}