In WPF, this was possible using FormattedText
, like this:
private Size MeasureString(string candidate)
{
var formattedText = new FormattedText(
candidate,
CultureInfo.CurrentUICulture,
FlowDirection.LeftToRight,
new Typeface(this.textBlock.FontFamily, this.textBlock.FontStyle, this.textBlock.FontWeight, this.textBlock.FontStretch),
this.textBlock.FontSize,
Brushes.Black);
return new Size(formattedText.Width, formattedText.Height);
}
But in UWP this class does not exist any more. So how is it possible to calculate text dimensions in universal windows platform?
If you are having issues in UWP with
Size
not resolving or working properly with double's. It is probably because you are usingSystem.Drawing.Size
.Use
Windows.Foundation.Size
instead.In UWP, you create a
TextBlock
, set its properties (likeText
,FontSize
), and then call itsMeasure
method and pass in infinite size.After that its
DesiredSize
property contains the size the TextBlock will have.Here is an alternative approach using Win2D:
You can use it like this:
Source