@@ -21,16 +21,16 @@ import androidx.appcompat.widget.AppCompatTextView
2121 * 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
2222 */
2323class 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
3434
3535 private interface SizeTester {
3636 /* *
@@ -45,19 +45,19 @@ class AutoResizeTextView @JvmOverloads constructor(context: Context, attrs: Attr
4545
4646 init {
4747 // 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 )
5252 // no value was assigned during construction
53- _maxLines = NO_LINE_LIMIT
53+ maxLines = NO_LINE_LIMIT
5454 // prepare size tester:
55- _sizeTester = object : SizeTester {
55+ sizeTester = object : SizeTester {
5656 internal val textRect = RectF ()
5757
5858 @TargetApi(Build .VERSION_CODES .JELLY_BEAN )
5959 override fun onTestSize (suggestedSize : Int , availableSpace : RectF ): Int {
60- _paint !! .textSize = suggestedSize.toFloat()
60+ textPaint !! .textSize = suggestedSize.toFloat()
6161 val transformationMethod = transformationMethod
6262 val text: String
6363 if (transformationMethod != null )
@@ -66,10 +66,10 @@ class AutoResizeTextView @JvmOverloads constructor(context: Context, attrs: Attr
6666 text = getText().toString()
6767 val singleLine = maxLines == 1
6868 if (singleLine) {
69- textRect.bottom = _paint !! .fontSpacing
70- textRect.right = _paint !! .measureText(text)
69+ textRect.bottom = textPaint !! .fontSpacing
70+ textRect.right = textPaint !! .measureText(text)
7171 } 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 )
7373 // return early if we have more lines
7474 if (maxLines != NO_LINE_LIMIT && layout.lineCount > maxLines)
7575 return 1
@@ -93,7 +93,7 @@ class AutoResizeTextView @JvmOverloads constructor(context: Context, attrs: Attr
9393 // else, too big
9494 }
9595 }
96- _initialized = true
96+ initialized = true
9797 }
9898
9999 fun isValidWordWrap (before : Char , after : Char ): Boolean {
@@ -111,38 +111,38 @@ class AutoResizeTextView @JvmOverloads constructor(context: Context, attrs: Attr
111111 }
112112
113113 override fun setTextSize (size : Float ) {
114- _maxTextSize = size
114+ maxTextSize = size
115115 adjustTextSize()
116116 }
117117
118118 override fun setMaxLines (maxLines : Int ) {
119119 super .setMaxLines(maxLines)
120- _maxLines = maxLines
120+ this .maxLines = maxLines
121121 adjustTextSize()
122122 }
123123
124124 override fun getMaxLines (): Int {
125- return _maxLines
125+ return maxLines
126126 }
127127
128128 override fun setSingleLine () {
129129 super .setSingleLine()
130- _maxLines = 1
130+ maxLines = 1
131131 adjustTextSize()
132132 }
133133
134134 override fun setSingleLine (singleLine : Boolean ) {
135135 super .setSingleLine(singleLine)
136136 if (singleLine)
137- _maxLines = 1
137+ maxLines = 1
138138 else
139- _maxLines = NO_LINE_LIMIT
139+ maxLines = NO_LINE_LIMIT
140140 adjustTextSize()
141141 }
142142
143143 override fun setLines (lines : Int ) {
144144 super .setLines(lines)
145- _maxLines = lines
145+ maxLines = lines
146146 adjustTextSize()
147147 }
148148
@@ -153,14 +153,14 @@ class AutoResizeTextView @JvmOverloads constructor(context: Context, attrs: Attr
153153 Resources .getSystem()
154154 else
155155 c.resources
156- _maxTextSize = TypedValue .applyDimension(unit, size, r.displayMetrics)
156+ maxTextSize = TypedValue .applyDimension(unit, size, r.displayMetrics)
157157 adjustTextSize()
158158 }
159159
160160 override fun setLineSpacing (add : Float , mult : Float ) {
161161 super .setLineSpacing(add, mult)
162- _spacingMult = mult
163- _spacingAdd = add
162+ spacingMult = mult
163+ spacingAdd = add
164164 }
165165
166166 /* *
@@ -169,7 +169,7 @@ class AutoResizeTextView @JvmOverloads constructor(context: Context, attrs: Attr
169169 * @param minTextSize
170170 */
171171 fun setMinTextSize (minTextSize : Float ) {
172- _minTextSize = minTextSize
172+ this .minTextSize = minTextSize
173173 adjustTextSize()
174174 }
175175
@@ -181,23 +181,23 @@ class AutoResizeTextView @JvmOverloads constructor(context: Context, attrs: Attr
181181 // @Override
182182 // public void run()
183183 // {
184- if (! _initialized )
184+ if (! initialized )
185185 return
186- val startSize = _minTextSize .toInt()
186+ val startSize = minTextSize .toInt()
187187 val heightLimit = measuredHeight - compoundPaddingBottom - compoundPaddingTop
188- _widthLimit = measuredWidth - compoundPaddingLeft - compoundPaddingRight
189- if (_widthLimit <= 0 )
188+ widthLimit = measuredWidth - compoundPaddingLeft - compoundPaddingRight
189+ if (widthLimit <= 0 )
190190 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()
194194 superSetTextSize(startSize)
195195 // }
196196 // });
197197 }
198198
199199 private fun superSetTextSize (startSize : Int ) {
200- val textSize = binarySearch(startSize, _maxTextSize .toInt(), _sizeTester , _availableSpaceRect )
200+ val textSize = binarySearch(startSize, maxTextSize .toInt(), sizeTester, availableSpaceRect )
201201 super .setTextSize(TypedValue .COMPLEX_UNIT_PX , textSize.toFloat())
202202 }
203203
0 commit comments