From 58daa9390b185bb3bc15443c9437f9d69ae2dc92 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Mon, 14 May 2018 15:28:37 +0100 Subject: [PATCH] Allow CreateFormattedText to have the string it renders overridden. --- src/Avalonia.Controls/Presenters/TextPresenter.cs | 11 ++++++++--- src/Avalonia.Controls/Primitives/AccessText.cs | 11 ++--------- src/Avalonia.Controls/TextBlock.cs | 7 ++++--- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/Avalonia.Controls/Presenters/TextPresenter.cs b/src/Avalonia.Controls/Presenters/TextPresenter.cs index 902f951146..4d63148b5f 100644 --- a/src/Avalonia.Controls/Presenters/TextPresenter.cs +++ b/src/Avalonia.Controls/Presenters/TextPresenter.cs @@ -211,14 +211,19 @@ namespace Avalonia.Controls.Presenters } } - protected override FormattedText CreateFormattedText(Size constraint) + protected override FormattedText CreateFormattedText(Size constraint, string text) { + FormattedText result = null; + if (PasswordChar != default(char)) { - Text = new string(PasswordChar, Text.Length); + result = base.CreateFormattedText(constraint, new string(PasswordChar, Text.Length)); + } + else + { + result = base.CreateFormattedText(constraint, Text); } - var result = base.CreateFormattedText(constraint); var selectionStart = SelectionStart; var selectionEnd = SelectionEnd; var start = Math.Min(selectionStart, selectionEnd); diff --git a/src/Avalonia.Controls/Primitives/AccessText.cs b/src/Avalonia.Controls/Primitives/AccessText.cs index 4bb80e6d3f..5b0f79c9c8 100644 --- a/src/Avalonia.Controls/Primitives/AccessText.cs +++ b/src/Avalonia.Controls/Primitives/AccessText.cs @@ -83,16 +83,9 @@ namespace Avalonia.Controls.Primitives /// /// The constraint of the text. /// A object. - protected override FormattedText CreateFormattedText(Size constraint) + protected override FormattedText CreateFormattedText(Size constraint, string text) { - return new FormattedText - { - Constraint = constraint, - Typeface = new Typeface(FontFamily, FontSize, FontStyle, FontWeight), - Text = StripAccessKey(Text), - TextAlignment = TextAlignment, - Wrapping = TextWrapping, - }; + return base.CreateFormattedText(constraint, StripAccessKey(text)); } /// diff --git a/src/Avalonia.Controls/TextBlock.cs b/src/Avalonia.Controls/TextBlock.cs index 88a9fe077d..35600a213c 100644 --- a/src/Avalonia.Controls/TextBlock.cs +++ b/src/Avalonia.Controls/TextBlock.cs @@ -197,7 +197,7 @@ namespace Avalonia.Controls { if (_formattedText == null) { - _formattedText = CreateFormattedText(_constraint); + _formattedText = CreateFormattedText(_constraint, Text); } return _formattedText; @@ -348,14 +348,15 @@ namespace Avalonia.Controls /// Creates the used to render the text. /// /// The constraint of the text. + /// The text to generated the for. /// A object. - protected virtual FormattedText CreateFormattedText(Size constraint) + protected virtual FormattedText CreateFormattedText(Size constraint, string text) { return new FormattedText { Constraint = constraint, Typeface = new Typeface(FontFamily, FontSize, FontStyle, FontWeight), - Text = Text ?? string.Empty, + Text = text ?? string.Empty, TextAlignment = TextAlignment, Wrapping = TextWrapping, };