我正在开发一个应用程序中的功能,允许用户保存他们喜欢的字体(系列,大小,下划线,粗体或斜体)。我应该首先说我在开发或二元开发的最初几年内没有使用枚举,或者在构造函数内部使用枚举,所以我对这方面的了解很少。
众所周知,设置新字体很简单。
Font font = new Font("Arial", FontStyle.Bold | FontStyle.Underline);
我的问题是,是否有一种干净的方法可以将下划线,粗体或斜体的任何一种组合传递给构造函数,这些组合可能不是它们,可能只是粗体,粗体或斜体等?
清洁对我来说不必做这样的事情。
if(myFont.Bold || myFont.Underline || myFont.Italic)
{
font = new Font("Arial", FontStyle.Bold | FontStyle.Underline | FontStyle.Italic);
}
else if(myFont.Bold || myFont.Underline)
{
font = new Font("Arial", FontStyle.Bold | FontStyle.Underline);
}
else if(myFont.Bold || myFont.Italic)
{
font = new Font("Arial", FontStyle.Bold | FontStyle.Italic);
}
......等等
答案 0 :(得分:1)
你可以这样做:
string fontName = "Arial";
FontStyle style = FontStyle.Regular;
if (myFont.Bold)
style |= FontStyle.Bold;
if (myFont.Underline)
style |= FontStyle.Underline;
if (myFont.Italic)
style |= FontStyle.Italic;
Font font = new Font(fontName, style);
答案 1 :(得分:0)
你不能拥有多个具有相同签名的构造函数,所以这是一个非启动者,除非你人为地创建伪参数来区分它们。
相反,您应该创建不同的静态方法,例如static MyClass CreateBoldItalic(),每个组合一个你想要的组合。然后,这些将使用您选择的组合实例化该类。
答案 2 :(得分:0)
你可以这样做。使用Font's constructor的重载或适合您的任何重载。
Font myFont = ...;//get your font
Font font = new Font("Arial", myFont.Size, myFont.Style);