代码之家  ›  专栏  ›  技术社区  ›  David

如何从字符串中获取字体?

  •  1
  • David  · 技术社区  · 15 年前

    System.Web.UI.DataVisualization.Charting.Chart

     <asp:Chart runat="server">
         <legends>
             <asp:Legend Font="Microsoft Sans Serif, 8.25pt, style=Bold"/>
         </legends>
     </asp:Chart>
    

    如何在代码隐藏中执行类似操作?

    chart.Legends[0].Font = Font.???("Microsoft Sans Serif, 8.25pt, style=Bold")
    
    3 回复  |  直到 15 年前
        1
  •  6
  •   Rex M    15 年前

    使用 one of the constructors System.Drawing.Font

    chart.Legends[0].Font = new Font("Microsoft Sans Serif", 
                                     8.25,
                                     FontStyle.Bold);
    

    确保包括 System.Drawing FontFamily , FontStyle 等)。

        2
  •  2
  •   Adam V    15 年前

    string[] fontStrings = "Microsoft Sans Serif, 8.25pt, style=Bold".Split(',');
    fontStrings[1] = fontStrings[1].Replace("pt", "");
    fontStrings[2] = fontStrings[2].Replace("style=", "");
    var font = new System.Drawing.Font(
      fontStrings[0],
      float.Parse(fontStrings[1]),
      ((FontStyle)Enum.Parse(typeof(FontStyle), fontStrings[2]))
    );
    

    编辑: 啊,我做得很辛苦。如果它不是动态的,那么其他答案比我嚼着绳子的回答要好得多。:)

        3
  •  1
  •   pmarflee    15 年前

    System.Drawing.Font

    chart.Legends[0].Font = new Font("Microsoft Sans Serif", 8.25, FontStyle.Bold);