简体   繁体   English

使用 Console.WriteLine (C#) 或 printfn (F#) 编写粗体文本?

[英]Write boldface text using Console.WriteLine (C#) or printfn (F#)?

Is there a quick way to write boldface text using Console.WriteLine ( C# ) or printfn ( F# )?有没有使用Console.WriteLine ( C# ) 或printfn ( F# ) 编写粗体文本的快速方法?

If not boldface, perhaps some other kind of visual differentiator, like underlined text?如果不是粗体,也许是其他类型的视觉差异化因素,比如带下划线的文字?

You can set the Console.ForegroundColor . 您可以设置Console.ForegroundColor Click the link, there is a very good example provided in the MSDN. 单击链接,MSDN中提供了一个非常好的示例。

Fonts and Styles are not available on a per-word basis. 字体和样式不是基于每个单词提供的。 To get bold, italics, underline or size, you would have to change all text in the Console. 要获得粗体,斜体,下划线或大小,您必须更改控制台中的所有文本。

Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Test Text");

Console.ForegroundColor = ConsoleColor.Blue;
Console.BackgroundColor = ConsoleColor.White;
Console.WriteLine("Test Text 2");

Console.ResetColor(); // To return colors back

Check out Manuel Riezebosch's library https://github.com/riezebosch/Crayon .查看 Manuel Riezebosch 的图书馆https://github.com/riezebosch/Crayon The library uses ANSI codes to get Bold, Underline, Reversed, Dim decorators to work on console output.该库使用 ANSI 代码使粗体、下划线、反转、暗淡装饰器在控制台 output 上工作。

using static Crayon.Output;

Console.WriteLine(Green($"green {Red($"{Bold("bold")} red")} green"));
Console.WriteLine(Bold().Green().Text($"starting green {Red("then red")} must be green again"));

A point to note is that, though ANSI codes work directly on Linux and Mac terminals, on Windows 10 it needs to be explicitly enabled and this is done by the library.需要注意的一点是,虽然 ANSI 代码可以直接在 Linux 和 Mac 终端上运行,但在 Windows 10 上需要明确启用,这是由库完成的。 https://superuser.com/questions/413073/windows-console-with-ansi-colors-handling https://superuser.com/questions/413073/windows-console-with-ansi-colors-handling

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM