C # use a Custom font (obtained from a file)
During software development, especially when developing WinForm programs, some special fonts are inevitable to beautify the interface. However, after development, when the program is moved to another machine, the interface is naturally "terrible" because the corresponding fonts may not be installed on these machines "! Of course, you can also use the program to automatically install the appropriate font, but I believe that most people, including me, hate writing messy files to the system disk. So we have this article. Now, you can directly enter the topic. In fact, it is easy to implement in. NET. In System. drawing. text; there is a PrivateFontCollection in the namespace to help us complete related operations. We use the AddFontFile (@ "c: \ font. ttf) method to read the font file into the program, and obtain the FontFamily through the Families [] array attribute of the instance. The Font can be instantiated to complete the Font setting. Code:
1 // path 2 string path = @ "C: \ font. ttf "; 3 // read the font file 4 PrivateFontCollectionpfc = new PrivateFontCollection (); 5 pfc. addFontFile (path); 6 // instantiate the Font 7 Font f = new Font (pfc. families [0], 16); 8 // set the font 9 richTextBox1.Font = f;
: