开发者

Get font attribute for code behind

The thing is, i need to use to function Graphics.measureString to know how long (in pixel) a string will be rendered on my page. The thing is, measureString needs to know what font family and font size (Verdana 11 for example) is used to be able to give me the width. For info, Graphics.measureStrings needs the string to measure, the font family and the font size, then it returns a SizeF object from which you can get the width attribute so you know what length it is on your webpage (it isn't always perfectly accurate though).

So, i was wondering if there was a way of getting that info from the p开发者_Python百科age for the code behind, or am i doomed to hard code it somewhere in my control or some constants class.

Thanks for future (helping) answers


If your control has runat="server" then you can use the Style property to get the font-family and font-size:

string fontFamily = myControl.Style["font-family"];
string fontSize =  myControl.Style["font-size"];

This will only work for inline styles though.


You can use this over Net Framework 4 with System.Web.dll; for example:

CssStyleCollection css = CssStyleTools.Create();
css.Value = "border-top:1px dotted #BBB;margin-top: 0px;font-size:12px";
Console.WriteLine(css["margin-top"]); //prints "0px"

and you can look at "Change Font Family and Font Size using HtmlAgilityPack".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜