物业'字体'是'ReadOnly' vb.net for new Font

时间:2016-03-07 13:53:52

标签: asp.net vb.net

我的代码有什么问题

    Dim privateFonts As New System.Drawing.Text.PrivateFontCollection()
    privateFonts.AddFontFile("C:\Documents and Settings\somefont.ttf")
    Dim font As New System.Drawing.Font(privateFonts.Families(0), 12)
    LBL_Test.Font = font

我在线有错误:LBL_Test.Font = font

LBL_test是这样的标准标签:

<asp:Label ID="LBL_Test" runat="server" Text="Label"></asp:Label>

2 个答案:

答案 0 :(得分:2)

由于您在网页中,因此无法像在WinForms或WPF中那样使用Font对象,而Font属性确实是the documentation shows只读:

  

Public Overridable ReadOnly Property Font As FontInfo

相反,您正在使用CSS和HTML样式,因此您可以使用这些属性设置样式,例如:

LBL_Test.Font.Name = "Verdana"
LBL_Test.Font.Bold = true

要做这种样式,你最好添加一个CSS类并使用样式表来做样式,例如:

LBL_Test.CssClass = "MyClass"

或者在你的标记中:

<asp:Label ID="LBL_Test" CssClass="MyClass" runat="server" Text="Label"></asp:Label>

然后在样式表中:

.MyClass {
    font-family: Verdana;
    font-weight: bold
}

说了这么多,看起来你想要使用某种自定义字体。为此,您需要在运行浏览器的计算机上安装它,或者使用a web font

@font-face {
    font-family: "MyCustomFont";
    src: url("http://your-server/somefont.ttf");
}

.MyClass {
    font-family: "MyCustomFont"
}

答案 1 :(得分:1)

LBL_Test.Styles.Add("font-family", "MyCustomFont")
相关问题