Browse Source

Allow CreateFormattedText to have the string it renders overridden.

Dan Walmsley 7 years ago
parent
commit
58daa9390b

+ 8 - 3
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);

+ 2 - 9
src/Avalonia.Controls/Primitives/AccessText.cs

@@ -83,16 +83,9 @@ namespace Avalonia.Controls.Primitives
         /// </summary>
         /// <param name="constraint">The constraint of the text.</param>
         /// <returns>A <see cref="FormattedText"/> object.</returns>
-        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));
         }
 
         /// <summary>

+ 4 - 3
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 <see cref="FormattedText"/> used to render the text.
         /// </summary>
         /// <param name="constraint">The constraint of the text.</param>
+        /// <param name="text">The text to generated the <see cref="FormattedText"/> for.</param>
         /// <returns>A <see cref="FormattedText"/> object.</returns>
-        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,
             };