@@ -47,7 +47,7 @@ public void onFontRetrieved(@NonNull Typeface typeface, boolean fontResolvedSync
47
47
if (fontResolvedSynchronously ) {
48
48
return ;
49
49
}
50
- textWidthDirty = true ;
50
+ textSizeDirty = true ;
51
51
TextDrawableDelegate textDrawableDelegate = delegate .get ();
52
52
if (textDrawableDelegate != null ) {
53
53
textDrawableDelegate .onTextSizeChange ();
@@ -56,7 +56,7 @@ public void onFontRetrieved(@NonNull Typeface typeface, boolean fontResolvedSync
56
56
57
57
@ Override
58
58
public void onFontRetrievalFailed (int reason ) {
59
- textWidthDirty = true ;
59
+ textSizeDirty = true ;
60
60
// Use fallback font.
61
61
TextDrawableDelegate textDrawableDelegate = delegate .get ();
62
62
if (textDrawableDelegate != null ) {
@@ -66,7 +66,8 @@ public void onFontRetrievalFailed(int reason) {
66
66
};
67
67
68
68
private float textWidth ;
69
- private boolean textWidthDirty = true ;
69
+ private float textHeight ;
70
+ private boolean textSizeDirty = true ;
70
71
@ Nullable private WeakReference <TextDrawableDelegate > delegate = new WeakReference <>(null );
71
72
@ Nullable private TextAppearance textAppearance ;
72
73
@@ -88,21 +89,29 @@ public TextPaint getTextPaint() {
88
89
}
89
90
90
91
public void setTextWidthDirty (boolean dirty ) {
91
- textWidthDirty = dirty ;
92
+ textSizeDirty = dirty ;
92
93
}
93
94
94
95
public boolean isTextWidthDirty () {
95
- return textWidthDirty ;
96
+ return textSizeDirty ;
97
+ }
98
+
99
+ public void setTextSizeDirty (boolean dirty ) {
100
+ textSizeDirty = dirty ;
101
+ }
102
+
103
+ private void refreshTextDimens (String text ) {
104
+ textWidth = calculateTextWidth (text );
105
+ textHeight = calculateTextHeight (text );
106
+ textSizeDirty = false ;
96
107
}
97
108
98
109
/** Returns the visual width of the {@code text} based on its current text appearance. */
99
110
public float getTextWidth (String text ) {
100
- if (!textWidthDirty ) {
111
+ if (!textSizeDirty ) {
101
112
return textWidth ;
102
113
}
103
-
104
- textWidth = calculateTextWidth (text );
105
- textWidthDirty = false ;
114
+ refreshTextDimens (text );
106
115
return textWidth ;
107
116
}
108
117
@@ -113,6 +122,22 @@ private float calculateTextWidth(@Nullable CharSequence charSequence) {
113
122
return textPaint .measureText (charSequence , 0 , charSequence .length ());
114
123
}
115
124
125
+ /** Returns the visual height of the {@code text} based on its current text appearance. */
126
+ public float getTextHeight (@ Nullable String text ) {
127
+ if (!textSizeDirty ) {
128
+ return textHeight ;
129
+ }
130
+ refreshTextDimens (text );
131
+ return textHeight ;
132
+ }
133
+
134
+ private float calculateTextHeight (@ Nullable String str ) {
135
+ if (str == null ) {
136
+ return 0f ;
137
+ }
138
+ return Math .abs (textPaint .getFontMetrics ().ascent );
139
+ }
140
+
116
141
/**
117
142
* Returns the text appearance.
118
143
*
@@ -141,7 +166,7 @@ public void setTextAppearance(@Nullable TextAppearance textAppearance, Context c
141
166
textPaint .drawableState = textDrawableDelegate .getState ();
142
167
}
143
168
textAppearance .updateDrawState (context , textPaint , fontCallback );
144
- textWidthDirty = true ;
169
+ textSizeDirty = true ;
145
170
}
146
171
147
172
TextDrawableDelegate textDrawableDelegate = delegate .get ();
0 commit comments