diff --git a/src/Avalonia.Controls/Presenters/TextPresenter.cs b/src/Avalonia.Controls/Presenters/TextPresenter.cs index 622727ccbe9..e0cb22607dd 100644 --- a/src/Avalonia.Controls/Presenters/TextPresenter.cs +++ b/src/Avalonia.Controls/Presenters/TextPresenter.cs @@ -634,6 +634,8 @@ protected override Size MeasureOverride(Size availableSize) protected override Size ArrangeOverride(Size finalSize) { + var finalWidth = finalSize.Width; + var textWidth = Math.Ceiling(TextLayout.OverhangLeading + TextLayout.WidthIncludingTrailingWhitespace + TextLayout.OverhangTrailing); if (finalSize.Width < textWidth) @@ -641,15 +643,17 @@ protected override Size ArrangeOverride(Size finalSize) finalSize = finalSize.WithWidth(textWidth); } - if (MathUtilities.AreClose(_constraint.Width, finalSize.Width)) + // Check if the '_constraint' has changed since the last measure, + // if so recalculate the TextLayout according to the new size + // NOTE: It is important to check this against the actual final size + // (excluding the trailing whitespace) to avoid TextLayout overflow. + if (MathUtilities.AreClose(_constraint.Width, finalWidth) == false) { - return finalSize; - } - - _constraint = new Size(Math.Ceiling(finalSize.Width), double.PositiveInfinity); + _constraint = new Size(Math.Ceiling(finalWidth), double.PositiveInfinity); - _textLayout?.Dispose(); - _textLayout = null; + _textLayout?.Dispose(); + _textLayout = null; + } return finalSize; }