Guest User

Untitled

a guest
Aug 23rd, 2021
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.90 KB | None | 0 0
  1.     private val textPaint = TextPaint()
  2.     private var text: String?
  3.     private var staticLayout: StaticLayout
  4.  
  5.     init {
  6.        
  7.         //...
  8.  
  9.         staticLayout = StaticLayout(
  10.             text,
  11.             0,
  12.             text?.length!!,
  13.             textPaint,
  14.             textPaint.measureText(text).toInt(),
  15.             textAlignment,
  16.             1f,
  17.             0f,
  18.             false,
  19.              TextUtils.TruncateAt.END,
  20.             Integer.MAX_VALUE
  21.         )
  22.     }
  23.  
  24.     override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
  25.         val requiredWidthMode = MeasureSpec.getMode(widthMeasureSpec)
  26.         val requiredWidth = MeasureSpec.getSize(widthMeasureSpec)
  27.  
  28.         val width = if (requiredWidthMode == MeasureSpec.EXACTLY) {
  29.             requiredWidth
  30.         } else {
  31.             val desiredWidth = paddingStart + paddingEnd + staticLayout.width
  32.             if (requiredWidthMode == MeasureSpec.AT_MOST) {
  33.                 requiredWidth.coerceAtMost(desiredWidth)
  34.             }
  35.             desiredWidth
  36.         }
  37.  
  38.         staticLayout = StaticLayout(
  39.             text,
  40.             0,
  41.             text?.length!!,
  42.             textPaint,
  43.             width-paddingEnd-paddingStart,
  44.             textAlignment,
  45.             1f,
  46.             0f,
  47.             false, TextUtils.TruncateAt.END,
  48.             Integer.MAX_VALUE
  49.         )
  50.  
  51.         val requiredHeightMode = MeasureSpec.getMode(heightMeasureSpec)
  52.         val requiredHeight = MeasureSpec.getSize(heightMeasureSpec)
  53.         val height = if (requiredHeightMode == MeasureSpec.EXACTLY) {
  54.             requiredHeight
  55.         } else {
  56.             val desiredHeight = paddingTop + paddingBottom + staticLayout.height
  57.             if (requiredHeightMode == MeasureSpec.AT_MOST) {
  58.                 desiredHeight.coerceAtMost(requiredHeight)
  59.             }
  60.             desiredHeight
  61.         }
  62.  
  63.         setMeasuredDimension(width, height)
  64.     }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment