Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private val textPaint = TextPaint()
- private var text: String?
- private var staticLayout: StaticLayout
- init {
- //...
- staticLayout = StaticLayout(
- text,
- 0,
- text?.length!!,
- textPaint,
- textPaint.measureText(text).toInt(),
- textAlignment,
- 1f,
- 0f,
- false,
- TextUtils.TruncateAt.END,
- Integer.MAX_VALUE
- )
- }
- override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
- val requiredWidthMode = MeasureSpec.getMode(widthMeasureSpec)
- val requiredWidth = MeasureSpec.getSize(widthMeasureSpec)
- val width = if (requiredWidthMode == MeasureSpec.EXACTLY) {
- requiredWidth
- } else {
- val desiredWidth = paddingStart + paddingEnd + staticLayout.width
- if (requiredWidthMode == MeasureSpec.AT_MOST) {
- requiredWidth.coerceAtMost(desiredWidth)
- }
- desiredWidth
- }
- staticLayout = StaticLayout(
- text,
- 0,
- text?.length!!,
- textPaint,
- width-paddingEnd-paddingStart,
- textAlignment,
- 1f,
- 0f,
- false, TextUtils.TruncateAt.END,
- Integer.MAX_VALUE
- )
- val requiredHeightMode = MeasureSpec.getMode(heightMeasureSpec)
- val requiredHeight = MeasureSpec.getSize(heightMeasureSpec)
- val height = if (requiredHeightMode == MeasureSpec.EXACTLY) {
- requiredHeight
- } else {
- val desiredHeight = paddingTop + paddingBottom + staticLayout.height
- if (requiredHeightMode == MeasureSpec.AT_MOST) {
- desiredHeight.coerceAtMost(requiredHeight)
- }
- desiredHeight
- }
- setMeasuredDimension(width, height)
- }
Advertisement
Add Comment
Please, Sign In to add comment