Created
October 20, 2022 00:23
-
-
Save snowpero/6b8ba2d459165e704cacd3eb8bf619d5 to your computer and use it in GitHub Desktop.
SpannableString 을 사용할 경우 ellipsize=end 설정이 적용되지 않는 경우
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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