@@ -11,6 +11,47 @@ Item {
1111 property string expression: " 0"
1212 property color textColor: " #FFFFFF"
1313 property int baseFontSize: 48
14+ property int minFontSize: 12
15+
16+ property int effectiveFontSize: baseFontSize
17+
18+ TextMetrics {
19+ id: textMetrics
20+ font .pixelSize : root .baseFontSize
21+ font .family : " SF Pro Display"
22+ text: root .expression
23+ }
24+
25+ onExpressionChanged: recalculateFontSize ()
26+ onWidthChanged: recalculateFontSize ()
27+ onBaseFontSizeChanged: recalculateFontSize ()
28+
29+ Component .onCompleted : recalculateFontSize ()
30+
31+ function recalculateFontSize () {
32+ if (width <= 0 || expression .length === 0 ) {
33+ effectiveFontSize = baseFontSize
34+ return
35+ }
36+
37+ var size = baseFontSize
38+ var availableWidth = width - 16 // небольшой отступ
39+
40+ var estimatedCharWidth = size * 0.55
41+ var textLength = expression .length
42+
43+ while (size > minFontSize) {
44+ estimatedCharWidth = size * 0.55
45+ var estimatedWidth = textLength * estimatedCharWidth
46+
47+ if (estimatedWidth <= availableWidth) {
48+ break
49+ }
50+ size -= 2
51+ }
52+
53+ effectiveFontSize = Math .max (size, minFontSize)
54+ }
1455
1556 function parseExpression (expr ) {
1657 var tokens = []
@@ -150,7 +191,7 @@ Item {
150191
151192 Text {
152193 text: tokenData .value
153- font .pixelSize : root .baseFontSize
194+ font .pixelSize : root .effectiveFontSize
154195 font .family : " SF Pro Display"
155196 color: root .textColor
156197 verticalAlignment: Text .AlignVCenter
@@ -168,7 +209,7 @@ Item {
168209 Text {
169210 id: numeratorText
170211 text: tokenData .numerator
171- font .pixelSize : root .baseFontSize * 0.6
212+ font .pixelSize : root .effectiveFontSize * 0.6
172213 font .family : " SF Pro Display"
173214 color: root .textColor
174215 horizontalAlignment: Text .AlignHCenter
@@ -185,7 +226,7 @@ Item {
185226 Text {
186227 id: denominatorText
187228 text: tokenData .denominator
188- font .pixelSize : root .baseFontSize * 0.6
229+ font .pixelSize : root .effectiveFontSize * 0.6
189230 font .family : " SF Pro Display"
190231 color: root .textColor
191232 horizontalAlignment: Text .AlignHCenter
@@ -207,7 +248,7 @@ Item {
207248 }
208249 return result
209250 }
210- font .pixelSize : root .baseFontSize * 0.7
251+ font .pixelSize : root .effectiveFontSize * 0.7
211252 font .family : " SF Pro Display"
212253 color: root .textColor
213254 verticalAlignment: Text .AlignTop
@@ -220,7 +261,7 @@ Item {
220261
221262 Text {
222263 text: tokenData .value
223- font .pixelSize : root .baseFontSize
264+ font .pixelSize : root .effectiveFontSize
224265 font .family : " SF Pro Display"
225266 color: " #007AFF"
226267 verticalAlignment: Text .AlignVCenter
@@ -233,7 +274,7 @@ Item {
233274
234275 Text {
235276 text: tokenData .value
236- font .pixelSize : root .baseFontSize
277+ font .pixelSize : root .effectiveFontSize
237278 font .family : " SF Pro Display"
238279 color: " #007AFF"
239280 verticalAlignment: Text .AlignVCenter
0 commit comments