@@ -31,7 +31,7 @@ public static void SetFill(DependencyObject obj, bool value)
31
31
32
32
public static void OnFillChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
33
33
{
34
- if ( ! ( d is TextBlock textBlock ) )
34
+ if ( d is not TextBlock textBlock )
35
35
{
36
36
return ;
37
37
}
@@ -48,39 +48,25 @@ public static void OnFillChanged(DependencyObject d, DependencyPropertyChangedEv
48
48
}
49
49
}
50
50
51
- public static FrameworkElement GetParent ( DependencyObject obj )
52
- {
53
- return ( FrameworkElement ) obj . GetValue ( ParentProperty ) ;
54
- }
51
+ public static FrameworkElement GetParent ( DependencyObject obj ) => ( FrameworkElement ) obj . GetValue ( ParentProperty ) ;
55
52
56
- public static void SetParent ( DependencyObject obj , FrameworkElement value )
57
- {
58
- obj . SetValue ( ParentProperty , value ) ;
59
- }
53
+ public static void SetParent ( DependencyObject obj , FrameworkElement value ) => obj . SetValue ( ParentProperty , value ) ;
60
54
61
- // Using a DependencyProperty as the backing store for Parent. This enables animation, styling, binding, etc...
62
55
public static readonly DependencyProperty ParentProperty =
63
56
DependencyProperty . RegisterAttached ( "Parent" , typeof ( FrameworkElement ) , typeof ( FillManager ) , new UIPropertyMetadata ( null ) ) ;
64
-
65
- public static SizeChangedEventHandler GetHandler ( DependencyObject obj )
66
- {
67
- return ( SizeChangedEventHandler ) obj . GetValue ( HandlerProperty ) ;
68
- }
69
57
70
- public static void SetHandler ( DependencyObject obj , SizeChangedEventHandler value )
71
- {
72
- obj . SetValue ( HandlerProperty , value ) ;
73
- }
58
+ public static SizeChangedEventHandler GetHandler ( DependencyObject obj ) => ( SizeChangedEventHandler ) obj . GetValue ( HandlerProperty ) ;
59
+
60
+ public static void SetHandler ( DependencyObject obj , SizeChangedEventHandler value ) => obj . SetValue ( HandlerProperty , value ) ;
74
61
75
- // Using a DependencyProperty as the backing store for Handler. This enables animation, styling, binding, etc...
76
62
public static readonly DependencyProperty HandlerProperty =
77
63
DependencyProperty . RegisterAttached ( "Handler" , typeof ( SizeChangedEventHandler ) , typeof ( FillManager ) , new UIPropertyMetadata ( null ) ) ;
78
64
79
65
private static void TextBlock_Loaded ( object sender , RoutedEventArgs e )
80
66
{
81
67
var textBlock = ( TextBlock ) sender ;
82
68
83
- if ( ! ( VisualTreeHelper . GetParent ( textBlock ) is FrameworkElement parent ) )
69
+ if ( VisualTreeHelper . GetParent ( textBlock ) is not FrameworkElement parent )
84
70
{
85
71
return ;
86
72
}
@@ -113,39 +99,24 @@ private static void TextBlock_Unloaded(object sender, RoutedEventArgs e)
113
99
TextDescriptor . RemoveValueChanged ( textBlock , MeasureFontSize ) ;
114
100
FontFamilyDescriptor . RemoveValueChanged ( textBlock , MeasureFontSize ) ;
115
101
}
116
-
117
- public static double GetMaxFontSize ( DependencyObject obj )
118
- {
119
- return ( double ) obj . GetValue ( MaxFontSizeProperty ) ;
120
- }
121
102
122
- public static void SetMaxFontSize ( DependencyObject obj , double value )
123
- {
124
- obj . SetValue ( MaxFontSizeProperty , value ) ;
125
- }
103
+ public static double GetMaxFontSize ( DependencyObject obj ) => ( double ) obj . GetValue ( MaxFontSizeProperty ) ;
104
+
105
+ public static void SetMaxFontSize ( DependencyObject obj , double value ) => obj . SetValue ( MaxFontSizeProperty , value ) ;
126
106
127
107
// Using a DependencyProperty as the backing store for MaxFontSize. This enables animation, styling, binding, etc...
128
108
public static readonly DependencyProperty MaxFontSizeProperty =
129
109
DependencyProperty . RegisterAttached ( "MaxFontSize" , typeof ( double ) , typeof ( FillManager ) , new UIPropertyMetadata ( 100.0 ) ) ;
130
-
131
- public static double GetInterlinyage ( DependencyObject obj )
132
- {
133
- return ( double ) obj . GetValue ( InterlinyageProperty ) ;
134
- }
135
110
136
- public static void SetInterlinyage ( DependencyObject obj , double value )
137
- {
138
- obj . SetValue ( InterlinyageProperty , value ) ;
139
- }
111
+ public static double GetInterlinyage ( DependencyObject obj ) => ( double ) obj . GetValue ( InterlinyageProperty ) ;
112
+
113
+ public static void SetInterlinyage ( DependencyObject obj , double value ) => obj . SetValue ( InterlinyageProperty , value ) ;
140
114
141
115
// Using a DependencyProperty as the backing store for Interlinyage. This enables animation, styling, binding, etc...
142
116
public static readonly DependencyProperty InterlinyageProperty =
143
117
DependencyProperty . RegisterAttached ( "Interlinyage" , typeof ( double ) , typeof ( FillManager ) , new UIPropertyMetadata ( 0.0 , OnInterlinyageChanged ) ) ;
144
118
145
- public static void OnInterlinyageChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
146
- {
147
- MeasureFontSize ( d , EventArgs . Empty ) ;
148
- }
119
+ public static void OnInterlinyageChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e ) => MeasureFontSize ( d , EventArgs . Empty ) ;
149
120
150
121
/// <summary>
151
122
/// Задаёт как можно больший размер шрифта текстового блока исходя из доступной для него области
@@ -155,7 +126,8 @@ public static void OnInterlinyageChanged(DependencyObject d, DependencyPropertyC
155
126
private static void MeasureFontSize ( object sender , EventArgs e )
156
127
{
157
128
var textBlock = sender as TextBlock ;
158
- if ( ! ( VisualTreeHelper . GetParent ( textBlock ) is FrameworkElement parent ) )
129
+
130
+ if ( VisualTreeHelper . GetParent ( textBlock ) is not FrameworkElement parent )
159
131
{
160
132
return ;
161
133
}
@@ -186,6 +158,7 @@ private static void MeasureFontSize(object sender, EventArgs e)
186
158
} ;
187
159
188
160
var lineHeight = GetInterlinyage ( textBlock ) ;
161
+
189
162
if ( lineHeight < textBlock . FontFamily . LineSpacing )
190
163
{
191
164
lineHeight = textBlock . FontFamily . LineSpacing ;
@@ -194,6 +167,7 @@ private static void MeasureFontSize(object sender, EventArgs e)
194
167
var coef = lineHeight / textBlock . FontFamily . LineSpacing ;
195
168
196
169
double fontSize ;
170
+
197
171
if ( textBlock . TextWrapping == TextWrapping . NoWrap )
198
172
{
199
173
fontSize = GetMaxFontSize ( textBlock ) ;
@@ -223,12 +197,18 @@ private static void MeasureFontSize(object sender, EventArgs e)
223
197
ft . SetFontSize ( fontSize ) ;
224
198
double textHeight = ft . Height * coef ;
225
199
226
- if ( fontSize > 1.0 && ( textHeight > height || ( textBlock . TextWrapping == TextWrapping . NoWrap ? ft . Width > width * 0.97 : ft . MinWidth > ft . MaxTextWidth ) ) )
200
+ if ( fontSize > 1.0
201
+ && ( textHeight > height
202
+ || ( textBlock . TextWrapping == TextWrapping . NoWrap
203
+ ? ft . Width > width * 0.97
204
+ : ft . MinWidth > ft . MaxTextWidth ) ) )
227
205
{
228
206
var lower = 1.0 ;
229
207
230
208
if ( textHeight > height && textBlock . TextWrapping == TextWrapping . NoWrap )
209
+ {
231
210
lower = Math . Max ( lower , ( textHeight - height ) / lineHeight ) ;
211
+ }
232
212
233
213
fontSize -= lower ;
234
214
continue ;
0 commit comments