@@ -21,16 +21,16 @@ import androidx.appcompat.widget.AppCompatTextView
21
21
* More info here: https://code.google.com/p/android/issues/detail?id=22493 and here in case you wish to fix it: http://stackoverflow.com/a/21851239/878126
22
22
*/
23
23
class AutoResizeTextView @JvmOverloads constructor(context : Context , attrs : AttributeSet ? = null , defStyle : Int = android.R .attr.textViewStyle) : AppCompatTextView(context, attrs, defStyle) {
24
- private val _availableSpaceRect = RectF ()
25
- private val _sizeTester : SizeTester
26
- private var _maxTextSize : Float = 0 .toFloat()
27
- private var _spacingMult = 1.0f
28
- private var _spacingAdd = 0.0f
29
- private var _minTextSize : Float = 0 .toFloat()
30
- private var _widthLimit : Int = 0
31
- private var _maxLines : Int = 0
32
- private var _initialized = false
33
- private var _paint : TextPaint ? = null
24
+ private val availableSpaceRect = RectF ()
25
+ private val sizeTester : SizeTester
26
+ private var maxTextSize : Float = 0 .toFloat()
27
+ private var spacingMult = 1.0f
28
+ private var spacingAdd = 0.0f
29
+ private var minTextSize : Float = 0 .toFloat()
30
+ private var widthLimit : Int = 0
31
+ private var maxLines : Int = 0
32
+ private var initialized = false
33
+ private var textPaint : TextPaint ? = null
34
34
35
35
private interface SizeTester {
36
36
/* *
@@ -45,19 +45,19 @@ class AutoResizeTextView @JvmOverloads constructor(context: Context, attrs: Attr
45
45
46
46
init {
47
47
// using the minimal recommended font size
48
- _minTextSize = TypedValue .applyDimension(TypedValue .COMPLEX_UNIT_SP , 12f , resources.displayMetrics)
49
- _maxTextSize = textSize
50
- _paint = TextPaint (paint)
51
- if (_maxLines == 0 )
48
+ minTextSize = TypedValue .applyDimension(TypedValue .COMPLEX_UNIT_SP , 12f , resources.displayMetrics)
49
+ maxTextSize = textSize
50
+ textPaint = TextPaint (paint)
51
+ if (maxLines == 0 )
52
52
// no value was assigned during construction
53
- _maxLines = NO_LINE_LIMIT
53
+ maxLines = NO_LINE_LIMIT
54
54
// prepare size tester:
55
- _sizeTester = object : SizeTester {
55
+ sizeTester = object : SizeTester {
56
56
internal val textRect = RectF ()
57
57
58
58
@TargetApi(Build .VERSION_CODES .JELLY_BEAN )
59
59
override fun onTestSize (suggestedSize : Int , availableSpace : RectF ): Int {
60
- _paint !! .textSize = suggestedSize.toFloat()
60
+ textPaint !! .textSize = suggestedSize.toFloat()
61
61
val transformationMethod = transformationMethod
62
62
val text: String
63
63
if (transformationMethod != null )
@@ -66,10 +66,10 @@ class AutoResizeTextView @JvmOverloads constructor(context: Context, attrs: Attr
66
66
text = getText().toString()
67
67
val singleLine = maxLines == 1
68
68
if (singleLine) {
69
- textRect.bottom = _paint !! .fontSpacing
70
- textRect.right = _paint !! .measureText(text)
69
+ textRect.bottom = textPaint !! .fontSpacing
70
+ textRect.right = textPaint !! .measureText(text)
71
71
} else {
72
- val layout = StaticLayout (text, _paint , _widthLimit , Alignment .ALIGN_NORMAL , _spacingMult , _spacingAdd , true )
72
+ val layout = StaticLayout (text, textPaint, widthLimit , Alignment .ALIGN_NORMAL , spacingMult, spacingAdd , true )
73
73
// return early if we have more lines
74
74
if (maxLines != NO_LINE_LIMIT && layout.lineCount > maxLines)
75
75
return 1
@@ -93,7 +93,7 @@ class AutoResizeTextView @JvmOverloads constructor(context: Context, attrs: Attr
93
93
// else, too big
94
94
}
95
95
}
96
- _initialized = true
96
+ initialized = true
97
97
}
98
98
99
99
fun isValidWordWrap (before : Char , after : Char ): Boolean {
@@ -111,38 +111,38 @@ class AutoResizeTextView @JvmOverloads constructor(context: Context, attrs: Attr
111
111
}
112
112
113
113
override fun setTextSize (size : Float ) {
114
- _maxTextSize = size
114
+ maxTextSize = size
115
115
adjustTextSize()
116
116
}
117
117
118
118
override fun setMaxLines (maxLines : Int ) {
119
119
super .setMaxLines(maxLines)
120
- _maxLines = maxLines
120
+ this .maxLines = maxLines
121
121
adjustTextSize()
122
122
}
123
123
124
124
override fun getMaxLines (): Int {
125
- return _maxLines
125
+ return maxLines
126
126
}
127
127
128
128
override fun setSingleLine () {
129
129
super .setSingleLine()
130
- _maxLines = 1
130
+ maxLines = 1
131
131
adjustTextSize()
132
132
}
133
133
134
134
override fun setSingleLine (singleLine : Boolean ) {
135
135
super .setSingleLine(singleLine)
136
136
if (singleLine)
137
- _maxLines = 1
137
+ maxLines = 1
138
138
else
139
- _maxLines = NO_LINE_LIMIT
139
+ maxLines = NO_LINE_LIMIT
140
140
adjustTextSize()
141
141
}
142
142
143
143
override fun setLines (lines : Int ) {
144
144
super .setLines(lines)
145
- _maxLines = lines
145
+ maxLines = lines
146
146
adjustTextSize()
147
147
}
148
148
@@ -153,14 +153,14 @@ class AutoResizeTextView @JvmOverloads constructor(context: Context, attrs: Attr
153
153
Resources .getSystem()
154
154
else
155
155
c.resources
156
- _maxTextSize = TypedValue .applyDimension(unit, size, r.displayMetrics)
156
+ maxTextSize = TypedValue .applyDimension(unit, size, r.displayMetrics)
157
157
adjustTextSize()
158
158
}
159
159
160
160
override fun setLineSpacing (add : Float , mult : Float ) {
161
161
super .setLineSpacing(add, mult)
162
- _spacingMult = mult
163
- _spacingAdd = add
162
+ spacingMult = mult
163
+ spacingAdd = add
164
164
}
165
165
166
166
/* *
@@ -169,7 +169,7 @@ class AutoResizeTextView @JvmOverloads constructor(context: Context, attrs: Attr
169
169
* @param minTextSize
170
170
*/
171
171
fun setMinTextSize (minTextSize : Float ) {
172
- _minTextSize = minTextSize
172
+ this .minTextSize = minTextSize
173
173
adjustTextSize()
174
174
}
175
175
@@ -181,23 +181,23 @@ class AutoResizeTextView @JvmOverloads constructor(context: Context, attrs: Attr
181
181
// @Override
182
182
// public void run()
183
183
// {
184
- if (! _initialized )
184
+ if (! initialized )
185
185
return
186
- val startSize = _minTextSize .toInt()
186
+ val startSize = minTextSize .toInt()
187
187
val heightLimit = measuredHeight - compoundPaddingBottom - compoundPaddingTop
188
- _widthLimit = measuredWidth - compoundPaddingLeft - compoundPaddingRight
189
- if (_widthLimit <= 0 )
188
+ widthLimit = measuredWidth - compoundPaddingLeft - compoundPaddingRight
189
+ if (widthLimit <= 0 )
190
190
return
191
- _paint = TextPaint (paint)
192
- _availableSpaceRect .right = _widthLimit .toFloat()
193
- _availableSpaceRect .bottom = heightLimit.toFloat()
191
+ textPaint = TextPaint (paint)
192
+ availableSpaceRect .right = widthLimit .toFloat()
193
+ availableSpaceRect .bottom = heightLimit.toFloat()
194
194
superSetTextSize(startSize)
195
195
// }
196
196
// });
197
197
}
198
198
199
199
private fun superSetTextSize (startSize : Int ) {
200
- val textSize = binarySearch(startSize, _maxTextSize .toInt(), _sizeTester , _availableSpaceRect )
200
+ val textSize = binarySearch(startSize, maxTextSize .toInt(), sizeTester, availableSpaceRect )
201
201
super .setTextSize(TypedValue .COMPLEX_UNIT_PX , textSize.toFloat())
202
202
}
203
203
0 commit comments