2012-04-21 12 views

उत्तर

16

पहला चरण SpriteFont.MeasureString() का उपयोग कर स्ट्रिंग को मापना है।

फिर, उदाहरण के लिए यदि आप इसे किसी निश्चित बिंदु के बाईं ओर खींचना चाहते हैं, तो डिफ़ॉल्ट के बजाय दाईं ओर, तो आपको टेक्स्ट ड्राइंग मूल से माप की एक्स चौड़ाई घटाएं। यदि आप चाहते हैं यह केंद्रित किया जाना है, तो आप आधे माप, आदि

15

उपयोग कर सकते हैं मैं इस कोड का उपयोग करें:

[Flags] 
public enum Alignment { Center=0, Left=1, Right=2, Top=4, Bottom = 8 } 

public void DrawString(SpriteFont font, string text, Rectangle bounds, Alignment align, Color color) 
    { 
     Vector2 size = font.MeasureString(text); 
     Vector2 pos = bounds.GetCenter(); 
     Vector2 origin = size*0.5f; 

     if (align.HasFlag(Alignment.Left)) 
      origin.X += bounds.Width/2 - size.X/2; 

     if (align.HasFlag(Alignment.Right)) 
      origin.X -= bounds.Width/2 - size.X/2; 

     if (align.HasFlag(Alignment.Top)) 
      origin.Y += bounds.Height/2 - size.Y/2; 

     if (align.HasFlag(Alignment.Bottom)) 
      origin.Y -= bounds.Height/2 - size.Y/2; 

     DrawString(font, text, pos, color, 0, origin, 1, SpriteEffects.None, 0); 
    } 
-3
SpriteFont mFont; 
SpriteBatch mSprite; 

mSprite.Begin(); 
mSprite.DrawString(mFont, "YourText", new Vector2(graphicsDevice.Viewport.Width/2 - mFont.MeasureString("YourText").Length()/2, 0), Color.White, 0, new Vector2(0, 0), 1f, SpriteEffects.None, 0f); 
mSprite.End(); 
संबंधित मुद्दे