7

我在 C#.NET4 控制台应用程序中将文本居中时遇到问题。

这是我将文本居中的方法:

private static void centerText(String text)
{
    int winWidth = (Console.WindowWidth / 2);
    Console.WriteLine(String.Format("{0,"+winWidth+"}", text));
}

但是,我只是得到输出,因为它本来可以正常输出。但是,如果我使用此行:

Console.WriteLine(String.Format("{0,"+winWidth+"}", "text"));

“文本”应该居中。

centerText用这两种方法打电话:

private static void drawStars()
{
    centerText("*********************************************");
}
private static void title(string location)
{
    drawStars();
    centerText("+++ Du er nu her: " + location + "! +++");
    drawStars();
}
4

3 回答 3

17

试试这个:

private static void centerText(String text)
{
    Console.Write(new string(' ', (Console.WindowWidth - text.Length) / 2));
    Console.WriteLine(text);
}

您的初始代码的问题是您的文本屏幕中心开始。您希望文本的中心在那里。

如果你想打印这样居中的整个段落,你需要做更多的工作。

于 2012-10-11T20:29:21.677 回答
3

我有自己的调用控制台标头的方法:

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("╚════════════════════════════════════════════════════════════════════════════════════════╝");
}

然后你这样称呼它YourStaticClass.Header("Test", "Version 1.0");

它应该是这样的:

╔════════════════════════════════════════════════════════════════════════════════════════╗
║                                          Test                                          ║
║                                      Version 1.0                                       ║
╚════════════════════════════════════════════════════════════════════════════════════════╝

您可以将90inwindowsWidth替换为Console.WindowWidth

更新- 2019 年 2 月 - 代码清理并动态大小

/// <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);
}
于 2018-02-12T20:55:41.263 回答
2

传入的文本可能有空格,例如\r\n,然后在调用 write 之前将其删除,例如

string textClean = Regex.Replace(text, @"([\r\n])", string.Empty);

// Then center on text clean 
于 2012-10-11T20:32:25.283 回答