diff --git a/src/Avalonia.Controls/SelectableTextBlock.cs b/src/Avalonia.Controls/SelectableTextBlock.cs index b0fd78b321e..cedb358fb30 100644 --- a/src/Avalonia.Controls/SelectableTextBlock.cs +++ b/src/Avalonia.Controls/SelectableTextBlock.cs @@ -213,6 +213,8 @@ protected override TextLayout CreateTextLayout(string? text) }; } + UpdateTextRuns(); + ITextSource textSource; if (_textRuns != null) diff --git a/src/Avalonia.Controls/TextBlock.cs b/src/Avalonia.Controls/TextBlock.cs index a8324b2a5a2..2d85e17f8ed 100644 --- a/src/Avalonia.Controls/TextBlock.cs +++ b/src/Avalonia.Controls/TextBlock.cs @@ -665,6 +665,8 @@ protected virtual TextLayout CreateTextLayout(string? text) { LineSpacing = LineSpacing }; + + UpdateTextRuns(); ITextSource textSource; @@ -722,10 +724,22 @@ protected override Size MeasureOverride(Size availableSize) //Force arrange so text will be properly alligned. InvalidateArrange(); } - + + UpdateTextRuns(true); + + //This implicitly recreated the TextLayout with a new constraint if we previously reset it. + var textLayout = TextLayout; + + var size = LayoutHelper.RoundLayoutSizeUp(new Size(textLayout.MinTextWidth, textLayout.Height).Inflate(padding), 1, 1); + + return size; + } + + protected void UpdateTextRuns(bool force = false) + { var inlines = Inlines; - if (HasComplexContent) + if (HasComplexContent && (force || _textRuns == null)) { var textRuns = new List(); @@ -736,13 +750,6 @@ protected override Size MeasureOverride(Size availableSize) _textRuns = textRuns; } - - //This implicitly recreated the TextLayout with a new constraint if we previously reset it. - var textLayout = TextLayout; - - var size = LayoutHelper.RoundLayoutSizeUp(new Size(textLayout.MinTextWidth, textLayout.Height).Inflate(padding), 1, 1); - - return size; } protected override Size ArrangeOverride(Size finalSize)