I am having a problem with centering text in a C#.NET4 console app.
This is my method for centering the text:
private static void centerText(String text)
{
int winWidth = (Console.WindowWidth / 2);
Console.WriteLine(String.Format("{0,"+winWidth+"}", text));
}
However, I just get the output as it would have been outputted normally. If I however use this line:
Console.WriteLine(String.Format("{0,"+winWidth+"}", "text"));
The "text" gets centered as it should.
I am calling centerText with these two methods:
private static void drawStars()
{
centerText("*********************************************");
}
private static void title(string location)
{
drawStars();
centerText("+++ Du er nu her: " + location + "! +++");
drawStars();
}
Try this instead:
private static void centerText(String text)
{
Console.Write(new string(' ', (Console.WindowWidth - text.Length) / 2));
Console.WriteLine(text);
}
The problem with your initial code was that your text starts in the screen center. You want the center of the text to be there.
You're going to do a bit more work if you want to print entire paragraphs centered like this.
The text passed in may have whitespace such as \r\n, then remove that before calling the write such as
string textClean = Regex.Replace(text, @"([\r\n])", string.Empty);
// Then center on text clean
I have my own method for calling console headers:
public static void Header(string title, string subtitle = "", ConsoleColor color = ConsoleColor.White)
{
int windowWidth = 90 - 2;
string titleContent = String.Format("║{0," + ((windowWidth / 2) + (title.Length / 2)) + "}{1," + (windowWidth - (windowWidth / 2) - (title.Length / 2) + 1) + "}", title, "║");
string subtitleContent = String.Format("║{0," + ((windowWidth / 2) + (subtitle.Length / 2)) + "}{1," + (windowWidth - (windowWidth / 2) - (subtitle.Length / 2) + 1) + "}", subtitle, "║");
Console.WriteLine("╔════════════════════════════════════════════════════════════════════════════════════════╗");
Console.WriteLine(titleContent);
if (!string.IsNullOrEmpty(subtitle))
{
Console.WriteLine(subtitleContent);
}
Console.WriteLine("╚════════════════════════════════════════════════════════════════════════════════════════╝");
}
Then you call it like this YourStaticClass.Header("Test", "Version 1.0");
it should look like this:
╔════════════════════════════════════════════════════════════════════════════════════════╗
║ Test ║
║ Version 1.0 ║
╚════════════════════════════════════════════════════════════════════════════════════════╝
You can replace the 90 in windowsWidth with Console.WindowWidth
UPDATE - February 2019 - code cleaned and made dynamic size
/// <summary>
/// Application header, also sets the console title
/// </summary>
/// <param name="title">Title of application</param>
/// <param name="subtitle">Subtitle of application</param>
/// <param name="foreGroundColor">Foreground color</param>
public static void Header(string title, string subtitle = "", ConsoleColor foreGroundColor = ConsoleColor.White, int windowWidthSize = 90)
{
Console.Title = title + (subtitle != "" ? " - " + subtitle : "");
string titleContent = CenterText(title, "║");
string subtitleContent = CenterText(subtitle, "║");
string borderLine = new String('═', windowWidthSize - 2);
Console.ForegroundColor = foreGroundColor;
Console.WriteLine($"╔{borderLine}╗");
Console.WriteLine(titleContent);
if (!string.IsNullOrEmpty(subtitle))
{
Console.WriteLine(subtitleContent);
}
Console.WriteLine($"╚{borderLine}╝");
Console.ResetColor();
}
/// <summary>
/// Align content to center for console. Can be used with decoration if used inside menu or header
/// </summary>
/// <param name="content">Content to center</param>
/// <param name="decorationString">Left and right decoration, default is empty/none</param>
/// <returns>Center aligned text</returns>
public static string CenterText(string content, string decorationString = "", int windowWidthSize = 90)
{
int windowWidth = windowWidthSize - (2 * decorationString.Length);
return String.Format(decorationString + "{0," + ((windowWidth / 2) + (content.Length / 2)) + "}{1," + (windowWidth - (windowWidth / 2) - (content.Length / 2) + decorationString.Length) + "}", content, decorationString);
}
来源:https://stackoverflow.com/questions/12847960/centering-text-in-c-sharp-console-app-only-working-with-some-input