Skip to content

Instantly share code, notes, and snippets.

@snowpero
Created October 20, 2022 00:23
Show Gist options
  • Save snowpero/6b8ba2d459165e704cacd3eb8bf619d5 to your computer and use it in GitHub Desktop.
Save snowpero/6b8ba2d459165e704cacd3eb8bf619d5 to your computer and use it in GitHub Desktop.
SpannableString 을 사용할 경우 ellipsize=end 설정이 적용되지 않는 경우
class EllipsizeTextView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null
) : AppCompatTextView(context, attrs) {
override fun onDraw(canvas: Canvas?) {
// maxLines 를 넘어갈 경우만 처리
if( lineCount >= maxLines ) {
val charSequence = text
val lastCharDown = layout.getLineVisibleEnd(maxLines - 1)
if( charSequence.length > lastCharDown ) {
val ssb = SpannableStringBuilder()
ssb.append(charSequence.subSequence(0, lastCharDown-4)).append("...")
text = ssb
}
}
super.onDraw(canvas)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment