SimpleText.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using System.ComponentModel;
  3. using System.Windows;
  4. using System.Windows.Documents;
  5. using System.Windows.Media;
  6. using HandyControl.Data;
  7. using HandyControl.Tools.Helper;
  8. namespace HandyControl.Controls;
  9. public class SimpleText : FrameworkElement
  10. {
  11. private FormattedText _formattedText;
  12. static SimpleText()
  13. {
  14. SnapsToDevicePixelsProperty.OverrideMetadata(typeof(SimpleText), new FrameworkPropertyMetadata(ValueBoxes.TrueBox));
  15. UseLayoutRoundingProperty.OverrideMetadata(typeof(SimpleText), new FrameworkPropertyMetadata(ValueBoxes.TrueBox));
  16. }
  17. public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
  18. nameof(Text), typeof(string), typeof(SimpleText), new FrameworkPropertyMetadata(
  19. string.Empty,
  20. FrameworkPropertyMetadataOptions.AffectsMeasure |
  21. FrameworkPropertyMetadataOptions.AffectsRender, OnFormattedTextInvalidated));
  22. public string Text
  23. {
  24. get => (string) GetValue(TextProperty);
  25. set => SetValue(TextProperty, value);
  26. }
  27. public static readonly DependencyProperty TextAlignmentProperty = DependencyProperty.Register(
  28. nameof(TextAlignment), typeof(TextAlignment), typeof(SimpleText),
  29. new PropertyMetadata(default(TextAlignment), OnFormattedTextUpdated));
  30. public TextAlignment TextAlignment
  31. {
  32. get => (TextAlignment) GetValue(TextAlignmentProperty);
  33. set => SetValue(TextAlignmentProperty, value);
  34. }
  35. public static readonly DependencyProperty TextTrimmingProperty = DependencyProperty.Register(
  36. nameof(TextTrimming), typeof(TextTrimming), typeof(SimpleText),
  37. new PropertyMetadata(default(TextTrimming), OnFormattedTextInvalidated));
  38. public TextTrimming TextTrimming
  39. {
  40. get => (TextTrimming) GetValue(TextTrimmingProperty);
  41. set => SetValue(TextTrimmingProperty, value);
  42. }
  43. public static readonly DependencyProperty TextWrappingProperty = DependencyProperty.Register(
  44. nameof(TextWrapping), typeof(TextWrapping), typeof(SimpleText),
  45. new PropertyMetadata(TextWrapping.NoWrap, OnFormattedTextInvalidated));
  46. public TextWrapping TextWrapping
  47. {
  48. get => (TextWrapping) GetValue(TextWrappingProperty);
  49. set => SetValue(TextWrappingProperty, value);
  50. }
  51. public static readonly DependencyProperty ForegroundProperty = DependencyProperty.Register(
  52. nameof(Foreground), typeof(Brush), typeof(SimpleText), new PropertyMetadata(Brushes.Black, OnFormattedTextUpdated));
  53. public Brush Foreground
  54. {
  55. get => (Brush) GetValue(ForegroundProperty);
  56. set => SetValue(ForegroundProperty, value);
  57. }
  58. public static readonly DependencyProperty FontFamilyProperty = TextElement.FontFamilyProperty.AddOwner(
  59. typeof(SimpleText),
  60. new FrameworkPropertyMetadata(OnFormattedTextUpdated));
  61. public FontFamily FontFamily
  62. {
  63. get => (FontFamily) GetValue(FontFamilyProperty);
  64. set => SetValue(FontFamilyProperty, value);
  65. }
  66. public static readonly DependencyProperty FontSizeProperty = TextElement.FontSizeProperty.AddOwner(
  67. typeof(SimpleText),
  68. new FrameworkPropertyMetadata(OnFormattedTextUpdated));
  69. [TypeConverter(typeof(FontSizeConverter))]
  70. public double FontSize
  71. {
  72. get => (double) GetValue(FontSizeProperty);
  73. set => SetValue(FontSizeProperty, value);
  74. }
  75. public static readonly DependencyProperty FontStretchProperty = TextElement.FontStretchProperty.AddOwner(
  76. typeof(SimpleText),
  77. new FrameworkPropertyMetadata(OnFormattedTextUpdated));
  78. public FontStretch FontStretch
  79. {
  80. get => (FontStretch) GetValue(FontStretchProperty);
  81. set => SetValue(FontStretchProperty, value);
  82. }
  83. public static readonly DependencyProperty FontStyleProperty = TextElement.FontStyleProperty.AddOwner(
  84. typeof(SimpleText),
  85. new FrameworkPropertyMetadata(OnFormattedTextUpdated));
  86. public FontStyle FontStyle
  87. {
  88. get => (FontStyle) GetValue(FontStyleProperty);
  89. set => SetValue(FontStyleProperty, value);
  90. }
  91. public static readonly DependencyProperty FontWeightProperty = TextElement.FontWeightProperty.AddOwner(
  92. typeof(SimpleText),
  93. new FrameworkPropertyMetadata(OnFormattedTextUpdated));
  94. public FontWeight FontWeight
  95. {
  96. get => (FontWeight) GetValue(FontWeightProperty);
  97. set => SetValue(FontWeightProperty, value);
  98. }
  99. protected override void OnRender(DrawingContext drawingContext) => drawingContext.DrawText(_formattedText, new Point());
  100. private void EnsureFormattedText()
  101. {
  102. if (_formattedText != null || Text == null)
  103. {
  104. return;
  105. }
  106. _formattedText = TextHelper.CreateFormattedText(Text, FlowDirection,
  107. new Typeface(FontFamily, FontStyle, FontWeight, FontStretch), FontSize);
  108. UpdateFormattedText();
  109. }
  110. private void UpdateFormattedText()
  111. {
  112. if (_formattedText == null)
  113. {
  114. return;
  115. }
  116. _formattedText.MaxLineCount = TextWrapping == TextWrapping.NoWrap ? 1 : int.MaxValue;
  117. _formattedText.TextAlignment = TextAlignment;
  118. _formattedText.Trimming = TextTrimming;
  119. _formattedText.SetFontSize(FontSize);
  120. _formattedText.SetFontStyle(FontStyle);
  121. _formattedText.SetFontWeight(FontWeight);
  122. _formattedText.SetFontFamily(FontFamily);
  123. _formattedText.SetFontStretch(FontStretch);
  124. _formattedText.SetForegroundBrush(Foreground);
  125. }
  126. private static void OnFormattedTextUpdated(DependencyObject d, DependencyPropertyChangedEventArgs e)
  127. {
  128. var outlinedTextBlock = (SimpleText) d;
  129. outlinedTextBlock.UpdateFormattedText();
  130. outlinedTextBlock.InvalidateMeasure();
  131. outlinedTextBlock.InvalidateVisual();
  132. }
  133. private static void OnFormattedTextInvalidated(DependencyObject d, DependencyPropertyChangedEventArgs e)
  134. {
  135. var outlinedTextBlock = (SimpleText) d;
  136. outlinedTextBlock._formattedText = null;
  137. outlinedTextBlock.InvalidateMeasure();
  138. outlinedTextBlock.InvalidateVisual();
  139. }
  140. protected override Size MeasureOverride(Size availableSize)
  141. {
  142. EnsureFormattedText();
  143. _formattedText.MaxTextWidth = Math.Min(3579139, availableSize.Width);
  144. _formattedText.MaxTextHeight = Math.Max(0.0001d, availableSize.Height);
  145. return new Size(_formattedText.Width, _formattedText.Height);
  146. }
  147. }