Click here to Skip to main content
11,414,561 members (67,832 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: XNA4.0
Hello dear friends I'm working on a project and my purpose is to create a TextBox for Xna, I don't know how i can draw a string in aligned in arectangle, cause i don't see any rectangle parameter in function Draw string?

SpriteFont font;
       public void fncWriteStringOnText(string myText,int FontSize,Vector2 position,Color mycolor)
       {
           string BaseDirectory = ".";
           string FontFileName;
           if (FontSize == 14)
           {
               FontFileName = BaseDirectory + "\\Font\\myfont14";
               font = content.Load<SpriteFont>(FontFileName);
           }
           else if (FontSize == 16)
           {
               FontFileName = BaseDirectory + "\\Font\\myfont16";
               font = content.Load<SpriteFont>(FontFileName);
           }
            else if  (FontSize == 18)
           {
               FontFileName = BaseDirectory + "\\Font\\myfont18";
               font = content.Load<SpriteFont>(FontFileName);
           }
           else if (FontSize == 20)
           {
               FontFileName = BaseDirectory + "\\Font\\myfonft20";
               font = content.Load<SpriteFont>(FontFileName);
           }
           else if (FontSize == 22)
           {
               FontFileName = BaseDirectory + "\\Font\\myfont22";
               font = content.Load<SpriteFont>(FontFileName);
           }
           <big>spriteBatch.DrawString(font,myText,position,mycolor);</big>
       }

Its just vector 2D for the position, in Drawstring function.
Posted 21-Jan-13 10:53am
Edited 21-Jan-13 10:55am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

As it wasn't any helper i was forced to write my code:

This is my Text struct:
public struct Text
{
    public int key;
    public string myText;
    public int FontSize;
    public Rectangle rect;
    public Color mycolor;
    public string strplaceOfUse;
    public bool ShouldItBeShownInThisScreen;
    public string wrappedString;
    public int HorizontalMargin;
}
This is initialization of my text:

public Text mytext;
mytext.key = 1;
           mytext.HorizontalMargin = 50;
           mytext.mycolor = Color.Blue;
           mytext.rect = new Rectangle((WindowsScreenWidth - 1000) / 2 + mytext.HorizontalMargin, (WindowsScreenHeight - 300) / 2 + 50, 1000 - 1 * mytext.HorizontalMargin, 300);
           mytext.strplaceOfUse = "AddNewWords_Word";
           mytext.ShouldItBeShownInThisScreen = false;
           mytext.myText = "Hello This is a Test and will take time to be practical,so do think about it and make it better and i know you will";
           mytext.FontSize = 22;
           mytext.wrappedString = WrapText(fncGivemeFont(mytext.FontSize), mytext.myText, mytext.rect.Width);
This is initializing the font
public SpriteFont fncGivemeFont(int fontsize)
       {
           string BaseDirectory = ".";
           SpriteFont myfont=null;
           switch (fontsize)
           {
               case 14:
               {
                   myfont = content.Load<SpriteFont>(BaseDirectory + "\\Font\\myfont14");
                   break;
               }
               case 16:
               {
                   myfont = content.Load<SpriteFont>(BaseDirectory + "\\Font\\myfont16");
                   break;
               }
               case 18:
               {
                   myfont = content.Load<SpriteFont>(BaseDirectory + "\\Font\\myfont18");
                   break;
               }
               case 20:
               {
                   myfont = content.Load<SpriteFont>(BaseDirectory + "\\Font\\myfont20");
                   break;
               }
               case 22:
               {
                   myfont = content.Load<SpriteFont>(BaseDirectory + "\\Font\\myfont22");
                   break;
               }
               default:
               {
                   myfont = content.Load<SpriteFont>(BaseDirectory + "\\Font\\myfont14");
                   break;
               }
           }
           return myfont;
       }
This is my alignment function:

 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();
        }
My drawing function:

 public Text[] myTexts=new Text[1];
//ArrayList=>MyTexts
         myTexts[0] = mytext;

public void DrawTexts(SpriteBatch mybatch)
       {
           for (int i = 0; i < myTexts.Length; i++)
           {
               if (myTexts[i].ShouldItBeShownInThisScreen)
               {
                   mybatch.DrawString(fncGivemeFont(myTexts[i].FontSize), myTexts[i].wrappedString,new Vector2(myTexts[i].rect.Left,myTexts[i].rect.Top),myTexts[i].mycolor);
               }
           }
       }
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,130
1 OriginalGriff 7,477
2 Maciej Los 3,710
3 Abhinav S 3,298
4 Peter Leow 3,084


Advertise | Privacy | Mobile
Web04 | 2.8.150427.4 | Last Updated 22 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100