C# 更改字体样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17759744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:25:23  来源:igfitidea点击:

Change Font Style

c#

提问by udarabibile

I have a label with text in Boldand Italic. I want to change those font properties through a button click.

我有一个带有BoldItalic文本的标签。我想通过单击按钮来更改这些字体属性。

I got to know of the code Label1.Font = new Font(Label1.Font, FontStyle.Regular);

我知道了代码 Label1.Font = new Font(Label1.Font, FontStyle.Regular);

But from this code it will undo both BOLD& ITALICproperties. I want only to remove boldproperty.....

但是从这段代码中,它将撤消BOLDITALIC属性。我只想删除粗体属性.....

Are there anything like fontsyle.bold = false?

有什么类似的fontsyle.bold = false吗?

采纳答案by Alexei Levenkov

Use Font.Styleof original font when creating new one, use & ~to flip styles

创建新字体时使用原始字体的Font.Style,用于& ~翻转样式

   label1.Font = new Font(label1.Font, label1.Font.Style & ~FontStyle.Bold);

回答by Ishan Jain

You can try this also --

你也可以试试这个——

label1.Font = new Font("Arial", 24,FontStyle.Bold);

or

或者

mainForm.lblName.Font = new Font("Arial", mainForm.lblName.Font.Size);

The constructor takes different parameters. see more

构造函数采用不同的参数。查看更多

回答by Diego

The best option is to use bitcodes and the XOR operator ^

最好的选择是使用位码和 XOR 运算符 ^

try this code:

试试这个代码:

Label1.Font = new Font(Label1.Font.Style ^ FontStyle.Regular);