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
Change Font Style
提问by udarabibile
I have a label with text in Boldand Italic. I want to change those font properties through a button click.
我有一个带有Bold和Italic文本的标签。我想通过单击按钮来更改这些字体属性。
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.....
但是从这段代码中,它将撤消BOLD和ITALIC属性。我只想删除粗体属性.....
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
回答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);