[英]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
以便輕松訪問所有相關項( FontFamily
, FontStyle
等)。
假設它總是以這種形式出現,您也許可以解析它:
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.