Guest User

Untitled

a guest
Sep 17th, 2019
96
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import android.widget.TextView
  2.  
  3. /**
  4. * sets an alpha on the lines after the Nth line, this could be useful for when animating a TextView to get smnaller and have less lines
  5. * alpha: the desired alpha
  6. * onLinesAfter: all lines after this line will have the color set on them (1 based index, i.e first line is 1 not 0)
  7. * forceColor: by default will use the color already set on the TextView, but you can over ride the color if needed
  8. * return: the spannable used, in case you need to remove it later
  9. */
  10. fun TextView.alphaOnLinesAfterN(alpha: Float, onLinesAfter: Int, @ColorInt forceColor: Int = this.currentTextColor): Spannable? {
  11.  
  12. layout?.let {
  13. if (it.lineCount > onLinesAfter) {
  14.  
  15. val newColor = Color.argb((alpha * 255).toInt(), forceColor.red, forceColor.green, forceColor.blue)
  16. var sp = text as Spannable
  17.  
  18. sp.setSpan(ForegroundColorSpan(newColor), it.getLineStart(onLinesAfter), length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE)
  19. text = sp
  20. return sp
  21. }
  22. }
  23. return null
  24. }
RAW Paste Data