简体   繁体   中英

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# )?

If not boldface, perhaps some other kind of visual differentiator, like underlined text?

You can set the Console.ForegroundColor . Click the link, there is a very good example provided in the 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 . The library uses ANSI codes to get Bold, Underline, Reversed, Dim decorators to work on console 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. https://superuser.com/questions/413073/windows-console-with-ansi-colors-handling

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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