Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
Die FontFamily-Klasse bietet die folgenden Methoden, mit denen verschiedene Schriftarteneigenschaften für eine bestimmte Kategorie-/Schriftschnittkombination abgerufen werden können:
- GetEmHeight(FontStyle)
- GetCellAscent(FontStyle)
- GetCellDescent(FontStyle)
- GetLineSpacing(FontStyle)
Alle von diesen Methoden zurückgegebenen Zahlen entsprechen Schriftentwurfseinheiten; sie sind daher unabhängig von Schriftgrad und Einheiten eines bestimmten Font-Objekts.
In der folgenden Abbildung sind die verschiedenen Schriftarteneigenschaften dargestellt.
.png)
Im folgenden Beispiel werden die Eigenschaften für den Schriftschnitt "Normal" der Schriftartenkategorie Arial angezeigt. Durch den Code wird außerdem ein (auf der Arial-Kategorie basierendes) Font-Objekt mit einem Schriftgrad von 16 Pixeln erstellt. Darüber hinaus werden die Eigenschaften für das jeweilige Font-Objekt (in Pixel) angezeigt.
Dim infoString As String = "" ' enough space for one line of output
Dim ascent As Integer ' font family ascent in design units
Dim ascentPixel As Single ' ascent converted to pixels
Dim descent As Integer ' font family descent in design units
Dim descentPixel As Single ' descent converted to pixels
Dim lineSpacing As Integer ' font family line spacing in design units
Dim lineSpacingPixel As Single ' line spacing converted to pixels
Dim fontFamily As New FontFamily("Arial")
Dim font As New Font( _
fontFamily, _
16, _
FontStyle.Regular, _
GraphicsUnit.Pixel)
Dim pointF As New PointF(10, 10)
Dim solidBrush As New SolidBrush(Color.Black)
' Display the font size in pixels.
infoString = "font.Size returns " + font.Size + "."
e.Graphics.DrawString(infoString, font, solidBrush, pointF)
' Move down one line.
pointF.Y += font.Height
' Display the font family em height in design units.
infoString = "fontFamily.GetEmHeight() returns " + _
fontFamily.GetEmHeight(FontStyle.Regular) + "."
e.Graphics.DrawString(infoString, font, solidBrush, pointF)
' Move down two lines.
pointF.Y += 2 * font.Height
' Display the ascent in design units and pixels.
ascent = fontFamily.GetCellAscent(FontStyle.Regular)
' 14.484375 = 16.0 * 1854 / 2048
ascentPixel = _
font.Size * ascent / fontFamily.GetEmHeight(FontStyle.Regular)
infoString = "The ascent is " + ascent + " design units, " + ascentPixel _
+ " pixels."
e.Graphics.DrawString(infoString, font, solidBrush, pointF)
' Move down one line.
pointF.Y += font.Height
' Display the descent in design units and pixels.
descent = fontFamily.GetCellDescent(FontStyle.Regular)
' 3.390625 = 16.0 * 434 / 2048
descentPixel = _
font.Size * descent / fontFamily.GetEmHeight(FontStyle.Regular)
infoString = "The descent is " + descent + " design units, " + _
descentPixel + " pixels."
e.Graphics.DrawString(infoString, font, solidBrush, pointF)
' Move down one line.
pointF.Y += font.Height
' Display the line spacing in design units and pixels.
lineSpacing = fontFamily.GetLineSpacing(FontStyle.Regular)
' 18.398438 = 16.0 * 2355 / 2048
lineSpacingPixel = _
font.Size * lineSpacing / fontFamily.GetEmHeight(FontStyle.Regular)
infoString = "The line spacing is " + lineSpacing + " design units, " + _
lineSpacingPixel + " pixels."
e.Graphics.DrawString(infoString, font, solidBrush, pointF)
[C#]
string infoString = ""; // enough space for one line of output
int ascent; // font family ascent in design units
float ascentPixel; // ascent converted to pixels
int descent; // font family descent in design units
float descentPixel; // descent converted to pixels
int lineSpacing; // font family line spacing in design units
float lineSpacingPixel; // line spacing converted to pixels
FontFamily fontFamily = new FontFamily("Arial");
Font font = new Font(
fontFamily,
16, FontStyle.Regular,
GraphicsUnit.Pixel);
PointF pointF = new PointF(10, 10);
SolidBrush solidBrush = new SolidBrush(Color.Black);
// Display the font size in pixels.
infoString = "font.Size returns " + font.Size + ".";
e.Graphics.DrawString(infoString, font, solidBrush, pointF);
// Move down one line.
pointF.Y += font.Height;
// Display the font family em height in design units.
infoString = "fontFamily.GetEmHeight() returns " +
fontFamily.GetEmHeight(FontStyle.Regular) + ".";
e.Graphics.DrawString(infoString, font, solidBrush, pointF);
// Move down two lines.
pointF.Y += 2 * font.Height;
// Display the ascent in design units and pixels.
ascent = fontFamily.GetCellAscent(FontStyle.Regular);
// 14.484375 = 16.0 * 1854 / 2048
ascentPixel =
font.Size * ascent / fontFamily.GetEmHeight(FontStyle.Regular);
infoString = "The ascent is " + ascent + " design units, " + ascentPixel +
" pixels.";
e.Graphics.DrawString(infoString, font, solidBrush, pointF);
// Move down one line.
pointF.Y += font.Height;
// Display the descent in design units and pixels.
descent = fontFamily.GetCellDescent(FontStyle.Regular);
// 3.390625 = 16.0 * 434 / 2048
descentPixel =
font.Size * descent / fontFamily.GetEmHeight(FontStyle.Regular);
infoString = "The descent is " + descent + " design units, " +
descentPixel + " pixels.";
e.Graphics.DrawString(infoString, font, solidBrush, pointF);
// Move down one line.
pointF.Y += font.Height;
// Display the line spacing in design units and pixels.
lineSpacing = fontFamily.GetLineSpacing(FontStyle.Regular);
// 18.398438 = 16.0 * 2355 / 2048
lineSpacingPixel =
font.Size * lineSpacing / fontFamily.GetEmHeight(FontStyle.Regular);
infoString = "The line spacing is " + lineSpacing + " design units, " +
lineSpacingPixel + " pixels.";
e.Graphics.DrawString(infoString, font, solidBrush, pointF);
In der folgenden Abbildung ist das Ergebnis des oben angegebenen Codes dargestellt.
.png)
Beachten Sie in der vorangehenden Abbildung die ersten beiden ausgegebenen Zeilen. Das Font-Objekt gibt den Schriftgrad 16 und das FontFamily-Objekt eine Gevierthöhe von 2.048 zurück. Diese beiden Zahlen (16 und 2.048) sind der Schlüssel für die Umwandlung von Schriftentwurfseinheiten in Einheiten (in diesem Fall Pixel) des Font-Objekts.
Beispielsweise können Sie die Oberlänge von Entwurfseinheiten in Pixel wie folgt konvertieren:
.png)
Im vorangehenden Code wird Text vertikal positioniert, indem der Y-Datenmember eines PointF-Objekts festgelegt wird. Die y-Koordinate wird für jede neue Textzeile durch font.Height erhöht. Die Height-Eigenschaft eines Font-Objekts gibt den Zeilenabstand (in Pixel) für das jeweilige Font-Objekt zurück. In diesem Beispiel gibt Height die Zahl 19 zurück. Beachten Sie, dass diese Zahl der Zahl (zur ganzen Zahl gerundet) entspricht, die durch Konvertieren der Zeilenabstandseigenschaften in Pixel ermittelt wird.
Beachten Sie, dass die Geviertgröße nicht die Summe aus Versalhöhe und Unterlänge ist. Die Summe aus Versalhöhe und Unterlänge wird als Zellenhöhe bezeichnet. Die Zellenhöhe abzüglich des inneren Zeilenabstands ist gleich der Geviertgröße. Die Zellenhöhe zuzüglich des äußeren Zeilenabstands ist gleich dem Zeilenvorschub.