簡體   English   中英

如何從字符串中獲取字體?

[英]How do I get a Font from a string?

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")

使用System.Drawing.Font類上的構造函數之一

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

確保包含System.Drawing以便輕松訪問所有相關項( FontFamilyFontStyle等)。

假設它總是以這種形式出現,您也許可以解析它:

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]))
);

編輯:啊,我很難做到。 如果不是動態的,那么其他答案要比我的字符串查詢好得多。 :)

使用System.Drawing.Font構造函數的以下重載:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM