m8
How would I make automatic font size scaling with a custom control?
double minFontSize = 4.0;
double maxFontSize = 32.0;
double bestSize = minFontSize;
for (double size = maxFontSize; size >= minFontSize; size -= 0.5)
{
var formattedText = new FormattedText(
yourText,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface(yourFontFamily, yourFontStyle, yourFontWeight, FontStretches.Normal),
size,
Brushes.Black,
new NumberSubstitution(),
1.0);
formattedText.MaxTextWidth = ActualWidth;
formattedText.MaxTextHeight = ActualHeight;
if (formattedText.Width <= ActualWidth && formattedText.Height <= ActualHeight)
{
bestSize = size;
break; // We found a size that fits!
}
}
// Apply the best font size
_FormattedText.SetFontSize(bestSize);
2 replies
✅ Is there a good way to add example input values to your OpenAPI spec?
2. Using ISchemaFilter for richer control
Create a custom schema filter to inject examples at runtime:
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
public class ExampleSchemaFilter : ISchemaFilter
{
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
if (context.Type == typeof(DateTime))
{
schema.Example = new OpenApiString("2025-01-01T00:00:00Z");
}
}
}
5 replies
✅ Is there a good way to add example input values to your OpenAPI spec?
Yes, in .NET 9 (or even .NET 6/7/8), you can add example values to your OpenAPI spec so that tools like Scalar Playground, Swagger UI, or Postman prefill example data like a UTC datetime — saving you from typing it manually each time.
1. Using [SwaggerSchema] or [Example] attributes (recommended in .NET 7+)
For a model class:
using Swashbuckle.AspNetCore.Annotations;
public class MyRequest
{
[SwaggerSchema(Description = "Date in UTC", Format = "date-time", Example = "2025-01-01T00:00:00Z")]
public DateTime Timestamp { get; set; }
}
5 replies