Hi there, im working on my game called 'AI.You' which is all text based, right now though I think im going to need some text wrapping since it looks silly breaking the middle of words etc like it does right now.
The code I have so far for the text wrapping method is:
private static void textWrapper(string line)
{
//Split the string into an array.
string[] sentances = line.Split('\n');
//Wordwrap each sentance that is longer than the window width at the space before the last word on that line.
foreach(string sentance in sentances)
{
if (sentance.Length > Console.WindowWidth)
{
}
}
}
What I want to do next is check where the last space ' ' happens and then create a new line there. It would then however need to check the rest of the line to make sure it hasn't done the same thing again and this is why I'm confused I don't really know how to set this up.
Any help would be great (simple where possible as im just starting out ;P)
Are you drawing your text with a spritefont?
sentence.length gives the length of the sentence in characters, not in pixels.
so if you are using a spritefont you can use the spriteFont.MeasureString(sentence) to give you the actual pixel length of the sentence
When you know the pixel length you can iterate through the individual words of the line until the length of the first line is smaller than the width of the Console. You can simply place the words in a seperate string. Rinse and repeat as they say, until you have a series of strings that are all shorter than the length of the console window