I got some code from this messageboard post that works nicely for me. What I wound up with, after some fiddling of my own, is the following:
public System.Drawing.SizeF MeasureString(string Message, DXFonts.DXFont Font, float Width, ContentAlignment Align)
{
SharpDX.DirectWrite.TextFormat textFormat = Font.GetFormat(Align);
SharpDX.DirectWrite.TextLayout layout =
new SharpDX.DirectWrite.TextLayout(DXManager.WriteFactory, Message, textFormat, Width, textFormat.FontSize);
return new System.Drawing.SizeF(layout.Metrics.Width, layout.Metrics.Height);
}
If you plug in the text, the font, the proposed width, and the alignment, it exports the size of a rectangle to hold the text. What you would be looking for, of course, is the height, but this includes the width since the text seldom fills the entire space.
Note: As suggested by a commenter, the code should actually be the following to Dispose() of the resources:
public System.Drawing.SizeF MeasureString(string Message, DXFonts.DXFont Font, float Width, ContentAlignment Align)
{
SharpDX.DirectWrite.TextFormat textFormat = Font.GetFormat(Align);
SharpDX.DirectWrite.TextLayout layout =
new SharpDX.DirectWrite.TextLayout(DXManager.WriteFactory, Message, textFormat, Width, textFormat.FontSize);
textFormat.Dispose(); // IMPORTANT! If you don't dispose your SharpDX resources, your program will crash after a while.
return new System.Drawing.SizeF(layout.Metrics.Width, layout.Metrics.Height);
}