From 7192fbe2c81356ac2c9b91f10e82cee3d8e0fbff Mon Sep 17 00:00:00 2001 From: bjchen Date: Fri, 8 Oct 2021 09:40:13 +0800 Subject: [PATCH] When static layout return 2 lines,but the text is only one line only(because some font setting make text is one line long with some extra space.) text[end] will trigger string index out of bounds exception. --- .../src/com/lb/auto_fit_textview/AutoResizeTextView.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/AutoFitTextViewLibrary/src/com/lb/auto_fit_textview/AutoResizeTextView.kt b/AutoFitTextViewLibrary/src/com/lb/auto_fit_textview/AutoResizeTextView.kt index df5689f..1e72576 100644 --- a/AutoFitTextViewLibrary/src/com/lb/auto_fit_textview/AutoResizeTextView.kt +++ b/AutoFitTextViewLibrary/src/com/lb/auto_fit_textview/AutoResizeTextView.kt @@ -80,7 +80,7 @@ class AutoResizeTextView @JvmOverloads constructor(context: Context, attrs: Attr 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])) + if (i < lineCount - 1 && end > 0 && !isValidWordWrap(text[end - 1])) return 1 if (maxWidth < layout.getLineRight(i) - layout.getLineLeft(i)) maxWidth = layout.getLineRight(i).toInt() - layout.getLineLeft(i).toInt() @@ -98,8 +98,8 @@ class AutoResizeTextView @JvmOverloads constructor(context: Context, attrs: Attr initialized = true } - fun isValidWordWrap(before: Char, after: Char): Boolean { - return before == ' ' || before == '-' + fun isValidWordWrap(c: Char): Boolean { + return c == ' ' || c == '-' } override fun setAllCaps(allCaps: Boolean) {