Struct TextMeasure
The size of text as MeasureText(string, ShapingOptions?) lays it out, in canvas units.
public readonly record struct TextMeasure : IEquatable<TextMeasure>
- Implements
- Inherited Members
Examples
// A label shrunk until it fits a 200-pixel button, then centred on it.
const string label = "Download the full report";
var box = new RectF(20, 20, 200, 40);
var font = new Font(face, 24);
while (font.MeasureText(label).Advance > box.Width - 16 && font.Size > 8)
{
font = new Font(face, font.Size - 1);
}
TextMeasure size = font.MeasureText(label);
var origin = new Vector2(
box.X + ((box.Width - size.Advance) / 2),
box.Y + ((box.Height - size.Height) / 2) + size.Ascent);
using Raster<Rgba8> image = Raster.Create(240, 80, Rgba8.White);
using (RasterCanvas<Rgba8> canvas = image.CreateCanvas())
{
canvas.FillRoundRect(new RoundRect(box, 8), new Rgba8(30, 90, 200, 255));
canvas.DrawText(label, origin, font, Rgba8.White);
}
Constructors
TextMeasure(float, TextBounds, int, float, float, float)
The size of text as MeasureText(string, ShapingOptions?) lays it out, in canvas units.
public TextMeasure(float Advance, TextBounds InkBounds, int LineCount, float Ascent, float Descent, float LineHeight)
Parameters
AdvancefloatThe widest line's advance: where the pen ends on that line.
InkBoundsTextBoundsThe exact box around the ink, relative to the first baseline's left end, y down.
LineCountintThe number of lines.
AscentfloatThe font's ascent, positive.
DescentfloatThe font's descent, negative.
LineHeightfloatThe distance from one baseline to the next.
Examples
// A label shrunk until it fits a 200-pixel button, then centred on it.
const string label = "Download the full report";
var box = new RectF(20, 20, 200, 40);
var font = new Font(face, 24);
while (font.MeasureText(label).Advance > box.Width - 16 && font.Size > 8)
{
font = new Font(face, font.Size - 1);
}
TextMeasure size = font.MeasureText(label);
var origin = new Vector2(
box.X + ((box.Width - size.Advance) / 2),
box.Y + ((box.Height - size.Height) / 2) + size.Ascent);
using Raster<Rgba8> image = Raster.Create(240, 80, Rgba8.White);
using (RasterCanvas<Rgba8> canvas = image.CreateCanvas())
{
canvas.FillRoundRect(new RoundRect(box, 8), new Rgba8(30, 90, 200, 255));
canvas.DrawText(label, origin, font, Rgba8.White);
}
Properties
Advance
The widest line's advance: where the pen ends on that line.
public float Advance { get; init; }
Property Value
Ascent
The font's ascent, positive.
public float Ascent { get; init; }
Property Value
Descent
The font's descent, negative.
public float Descent { get; init; }
Property Value
Height
Gets the height of the line boxes: the first line's ascent to the last line's descent.
public float Height { get; }
Property Value
InkBounds
The exact box around the ink, relative to the first baseline's left end, y down.
public TextBounds InkBounds { get; init; }
Property Value
LineCount
The number of lines.
public int LineCount { get; init; }
Property Value
LineHeight
The distance from one baseline to the next.
public float LineHeight { get; init; }