|
| 1 | +using System; |
| 2 | +using System.ComponentModel; |
| 3 | +using System.Globalization; |
| 4 | +using System.Windows; |
| 5 | +using System.Windows.Controls; |
| 6 | +using System.Windows.Interactivity; |
| 7 | +using System.Windows.Media; |
| 8 | + |
| 9 | +namespace Daybreak.Behaviors |
| 10 | +{ |
| 11 | + public class ScaleFontWithSize : Behavior<TextBlock> |
| 12 | + { |
| 13 | + public static readonly DependencyProperty MaxFontSizeProperty = DependencyProperty.Register("MaxFontSize", typeof(double), typeof(ScaleFontWithSize), new PropertyMetadata(12d)); |
| 14 | + |
| 15 | + public double MaxFontSize |
| 16 | + { |
| 17 | + get |
| 18 | + { |
| 19 | + return (double)this.GetValue(MaxFontSizeProperty); |
| 20 | + } |
| 21 | + set |
| 22 | + { |
| 23 | + this.SetValue(MaxFontSizeProperty, value); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + protected override void OnAttached() |
| 28 | + { |
| 29 | + base.OnAttached(); |
| 30 | + this.AssociatedObject.SizeChanged += (_, __) => this.CalculateFontSize(); |
| 31 | + DependencyPropertyDescriptor.FromProperty( |
| 32 | + TextBlock.TextProperty, typeof(TextBlock)).AddValueChanged(this.AssociatedObject, (_, __) => this.CalculateFontSize()); |
| 33 | + DependencyPropertyDescriptor.FromProperty( |
| 34 | + TextBlock.FontSizeProperty, typeof(TextBlock)).AddValueChanged(this.AssociatedObject, (_, __) => this.CalculateFontSize()); |
| 35 | + } |
| 36 | + |
| 37 | + private void CalculateFontSize() |
| 38 | + { |
| 39 | + var textMeasurement = this.MeasureText(this.AssociatedObject.FontSize); |
| 40 | + var maximumTextMeasurement = this.MeasureText(this.MaxFontSize); |
| 41 | + var desiredWidthFontSize = this.MaxFontSize; |
| 42 | + var desiredHeightFontSize = this.MaxFontSize; |
| 43 | + |
| 44 | + if (Math.Round(textMeasurement.Height) != Math.Round(this.AssociatedObject.ActualHeight)) |
| 45 | + { |
| 46 | + |
| 47 | + var scale = this.AssociatedObject.ActualHeight / maximumTextMeasurement.Height; |
| 48 | + desiredHeightFontSize = (this.MaxFontSize * scale) - 1; |
| 49 | + desiredHeightFontSize = desiredHeightFontSize <= this.MaxFontSize ? desiredHeightFontSize : this.MaxFontSize; |
| 50 | + } |
| 51 | + |
| 52 | + if (Math.Round(textMeasurement.Width) != Math.Round(this.AssociatedObject.ActualWidth)) |
| 53 | + { |
| 54 | + var scale = this.AssociatedObject.ActualWidth / maximumTextMeasurement.Width; |
| 55 | + desiredWidthFontSize = (this.MaxFontSize * scale) - 1; |
| 56 | + desiredWidthFontSize = desiredWidthFontSize <= this.MaxFontSize ? desiredWidthFontSize : this.MaxFontSize; |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + var desiredFontSize = Math.Min(desiredHeightFontSize, desiredWidthFontSize); |
| 61 | + |
| 62 | + if ((int)desiredFontSize != (int)this.AssociatedObject.FontSize && desiredFontSize > 0) |
| 63 | + { |
| 64 | + this.AssociatedObject.FontSize = desiredFontSize; |
| 65 | + return; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private Size MeasureText(double fontSize) |
| 70 | + { |
| 71 | + var formattedText = new FormattedText(this.AssociatedObject.Text, CultureInfo.CurrentUICulture, |
| 72 | + FlowDirection.LeftToRight, |
| 73 | + new Typeface(this.AssociatedObject.FontFamily, this.AssociatedObject.FontStyle, this.AssociatedObject.FontWeight, this.AssociatedObject.FontStretch), |
| 74 | + fontSize, Brushes.Black, VisualTreeHelper.GetDpi(this.AssociatedObject).PixelsPerDip); |
| 75 | + |
| 76 | + return new Size(formattedText.Width, formattedText.Height); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments