-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoResizeTextView.kt
226 lines (202 loc) · 8.03 KB
/
AutoResizeTextView.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import android.annotation.TargetApi
import android.content.Context
import android.content.res.Resources
import android.graphics.RectF
import android.graphics.Typeface
import android.os.Build
import android.text.Layout.Alignment
import android.text.StaticLayout
import android.text.TextPaint
import android.util.AttributeSet
import android.util.TypedValue
import androidx.appcompat.widget.AppCompatTextView
class AutoResizeTextView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyle: Int = android.R.attr.textViewStyle) : AppCompatTextView(context, attrs, defStyle) {
private val availableSpaceRect = RectF()
private val sizeTester: SizeTester
private var maxTextSize: Float = 0.toFloat()
private var spacingMult = 1.0f
private var spacingAdd = 0.0f
private var minTextSize: Float = 0.toFloat()
private var widthLimit: Int = 0
private var maxLines: Int = 0
private var initialized = false
private var textPaint: TextPaint? = null
private interface SizeTester {
/**
* @param suggestedSize Size of text to be tested
* @param availableSpace available space in which text must fit
* @return an integer < 0 if after applying `suggestedSize` to
* text, it takes less space than `availableSpace`, > 0
* otherwise
*/
fun onTestSize(suggestedSize: Int, availableSpace: RectF): Int
}
init {
// using the minimal recommended font size
minTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12f, resources.displayMetrics)
maxTextSize = textSize
textPaint = TextPaint(paint)
if (maxLines == 0)
// no value was assigned during construction
maxLines = NO_LINE_LIMIT
// prepare size tester:
sizeTester = object : SizeTester {
internal val textRect = RectF()
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
override fun onTestSize(suggestedSize: Int, availableSpace: RectF): Int {
textPaint!!.textSize = suggestedSize.toFloat()
val transformationMethod = transformationMethod
val text: String
if (transformationMethod != null)
text = transformationMethod.getTransformation(getText(), this@AutoResizeTextView).toString()
else
text = getText().toString()
val singleLine = maxLines == 1
if (singleLine) {
textRect.bottom = textPaint!!.fontSpacing
textRect.right = textPaint!!.measureText(text)
} else {
val layout: StaticLayout = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
StaticLayout.Builder.obtain(text, 0, text.length, textPaint!!, widthLimit).setLineSpacing(spacingAdd, spacingMult).setAlignment(Alignment.ALIGN_NORMAL).setIncludePad(true).build()
} else StaticLayout(text, textPaint, widthLimit, Alignment.ALIGN_NORMAL, spacingMult, spacingAdd, true)
// return early if we have more lines
if (maxLines != NO_LINE_LIMIT && layout.lineCount > maxLines)
return 1
textRect.bottom = layout.height.toFloat()
var maxWidth = -1
val lineCount = layout.lineCount
for (i in 0 until lineCount) {
val end = layout.getLineEnd(i)
if (i < lineCount - 1 && end > 0 && !isValidWordWrap(text[end - 1], text[end]))
return 1
if (maxWidth < layout.getLineRight(i) - layout.getLineLeft(i))
maxWidth = layout.getLineRight(i).toInt() - layout.getLineLeft(i).toInt()
}
//for (int i = 0; i < layout.getLineCount(); i++)
// if (maxWidth < layout.getLineRight(i) - layout.getLineLeft(i))
// maxWidth = (int) layout.getLineRight(i) - (int) layout.getLineLeft(i);
textRect.right = maxWidth.toFloat()
}
textRect.offsetTo(0f, 0f)
return if (availableSpace.contains(textRect)) -1 else 1
// else, too big
}
}
initialized = true
}
fun isValidWordWrap(before: Char, after: Char): Boolean {
return before == ' ' || before == '-'
}
override fun setAllCaps(allCaps: Boolean) {
super.setAllCaps(allCaps)
adjustTextSize()
}
override fun setTypeface(tf: Typeface?) {
super.setTypeface(tf)
adjustTextSize()
}
override fun setTextSize(size: Float) {
maxTextSize = size
adjustTextSize()
}
override fun setMaxLines(maxLines: Int) {
super.setMaxLines(maxLines)
this.maxLines = maxLines
adjustTextSize()
}
override fun getMaxLines(): Int {
return maxLines
}
override fun setSingleLine() {
super.setSingleLine()
maxLines = 1
adjustTextSize()
}
override fun setSingleLine(singleLine: Boolean) {
super.setSingleLine(singleLine)
if (singleLine)
maxLines = 1
else
maxLines = NO_LINE_LIMIT
adjustTextSize()
}
override fun setLines(lines: Int) {
super.setLines(lines)
maxLines = lines
adjustTextSize()
}
override fun setTextSize(unit: Int, size: Float) {
val c = context
val r: Resources
r = if (c == null)
Resources.getSystem()
else
c.resources
maxTextSize = TypedValue.applyDimension(unit, size, r.displayMetrics)
adjustTextSize()
}
override fun setLineSpacing(add: Float, mult: Float) {
super.setLineSpacing(add, mult)
spacingMult = mult
spacingAdd = add
}
/**
* Set the lower text size limit and invalidate the view
*
* @param minTextSize
*/
fun setMinTextSize(minTextSize: Float) {
this.minTextSize = minTextSize
adjustTextSize()
}
private fun adjustTextSize() {
if (!initialized)
return
val startSize = minTextSize.toInt()
val heightLimit = measuredHeight - compoundPaddingBottom - compoundPaddingTop
widthLimit = measuredWidth - compoundPaddingLeft - compoundPaddingRight
if (widthLimit <= 0)
return
textPaint = TextPaint(paint)
availableSpaceRect.right = widthLimit.toFloat()
availableSpaceRect.bottom = heightLimit.toFloat()
superSetTextSize(startSize)
}
private fun superSetTextSize(startSize: Int) {
val textSize = binarySearch(startSize, maxTextSize.toInt(), sizeTester, availableSpaceRect)
super.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize.toFloat())
}
private fun binarySearch(start: Int, end: Int, sizeTester: SizeTester, availableSpace: RectF): Int {
var lastBest = start
var lo = start
var hi = end - 1
var mid: Int
while (lo <= hi) {
mid = (lo + hi).ushr(1)
val midValCmp = sizeTester.onTestSize(mid, availableSpace)
if (midValCmp < 0) {
lastBest = lo
lo = mid + 1
} else if (midValCmp > 0) {
hi = mid - 1
lastBest = hi
} else
return mid
}
// make sure to return last best
// this is what should always be returned
return lastBest
}
override fun onTextChanged(text: CharSequence, start: Int, before: Int, after: Int) {
super.onTextChanged(text, start, before, after)
adjustTextSize()
}
override fun onSizeChanged(width: Int, height: Int, oldwidth: Int, oldheight: Int) {
super.onSizeChanged(width, height, oldwidth, oldheight)
if (width != oldwidth || height != oldheight)
adjustTextSize()
}
companion object {
private val NO_LINE_LIMIT = -1
}
}