Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import android.animation.LayoutTransition
import android.content.Context
import android.graphics.Color
import android.os.Handler
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.TextPaint
import android.text.*
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.View
Expand Down Expand Up @@ -79,7 +76,7 @@ class ShowMoreLess private constructor(builder: Builder) {

textView.post(Runnable {
try {
val trimText = text.toString().trim()
val trimText = trimText(text)
textView.text = trimText
if (trimText.isEmpty())
return@Runnable
Expand Down Expand Up @@ -280,6 +277,32 @@ class ShowMoreLess private constructor(builder: Builder) {
}
}

/**
* Support [SpannableStringBuilder] trim without loosing span added on it
*/
private fun trimText(text: CharSequence): CharSequence {
var length = text.length
val trimmedLength: Int = TextUtils.getTrimmedLength(text)
if (length > trimmedLength) {
val builder = SpannableStringBuilder(text)
// Remove white spaces from the start.
var start = 0
while (start < length && builder[start] <= ' ') {
start++
}
builder.delete(0, start)
length -= start
// Remove white spaces from the end.
var end = length
while (end >= 0 && builder[end - 1] <= ' ') {
end--
}
builder.delete(end, length)
return builder
}
return text
}

fun setListener(listener: OnShowMoreLessClickedListener) {
onShowMoreLessClickedListener = listener
}
Expand Down