Skip to content

Commit 8cee8f2

Browse files
committed
support text wrapping and centering
Prevent long texts to be covered by the slide icon button as well as gets cut off by supporting text-wrapping.
1 parent e1185de commit 8cee8f2

File tree

1 file changed

+47
-9
lines changed

1 file changed

+47
-9
lines changed

slidetoact/src/main/java/com/ncorti/slidetoact/SlideToActView.kt

+47-9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ import android.graphics.drawable.Drawable
1717
import android.os.Build
1818
import android.os.VibrationEffect
1919
import android.os.Vibrator
20+
import android.text.Layout
21+
import android.text.StaticLayout
22+
import android.text.TextPaint
23+
import android.text.TextUtils
2024
import android.util.AttributeSet
2125
import android.util.Log
2226
import android.util.TypedValue
@@ -38,6 +42,7 @@ import com.ncorti.slidetoact.SlideToActIconUtil.createIconAnimator
3842
import com.ncorti.slidetoact.SlideToActIconUtil.loadIconCompat
3943
import com.ncorti.slidetoact.SlideToActIconUtil.stopIconAnimation
4044
import com.ncorti.slidetoact.SlideToActIconUtil.tintIconCompat
45+
import kotlin.math.max
4146

4247
/**
4348
* Class representing the custom view, SlideToActView.
@@ -235,7 +240,7 @@ class SlideToActView @JvmOverloads constructor(
235240
private val mInnerPaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG)
236241

237242
/** Paint used for text elements */
238-
private var mTextPaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG)
243+
private var mTextPaint: TextPaint = TextPaint(Paint.ANTI_ALIAS_FLAG)
239244

240245
/** TextView used for text elements */
241246
private var mTextView: TextView
@@ -482,14 +487,47 @@ class SlideToActView @JvmOverloads constructor(
482487
mTextPaint.alpha = (255 * mPositionPercInv).toInt()
483488
// Checking if the TextView has a Transformation method applied (e.g. AllCaps).
484489
val textToDraw = mTextView.transformationMethod?.getTransformation(text, mTextView) ?: text
485-
canvas.drawText(
486-
textToDraw,
487-
0,
488-
textToDraw.length,
489-
mTextXPosition,
490-
mTextYPosition,
491-
mTextPaint
492-
)
490+
val leftOffset =
491+
(mAreaHeight - 2 * mActualAreaMargin).toFloat() / mAreaHeight.toFloat() *
492+
mBorderRadius.toFloat() * 2
493+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
494+
val maxWidth = mAreaWidth - (2 * mActualAreaWidth) - leftOffset
495+
val textLayout = StaticLayout.Builder
496+
.obtain(textToDraw, 0, textToDraw.length, mTextPaint, maxWidth.toInt())
497+
.setAlignment(Layout.Alignment.ALIGN_NORMAL)
498+
.setEllipsize(TextUtils.TruncateAt.END)
499+
.setEllipsizedWidth(maxWidth.toInt() - mTextPaint.textSize.toInt())
500+
.setMaxLines(
501+
max(
502+
1f,
503+
mAreaHeight / (mTextPaint.descent() - mTextPaint.ascent())
504+
).toInt()
505+
)
506+
.build()
507+
canvas.save()
508+
509+
val dY = (height / 2) - (textLayout.height / 2)
510+
val dX = (width / 2) - (textLayout.width / 2)
511+
512+
val mTextLeft = mActualAreaWidth + leftOffset + dX
513+
val mTextTop = 0 + dY
514+
val mTextRight = mAreaWidth - mActualAreaWidth - dX
515+
516+
canvas.translate(mTextLeft.toFloat(), mTextTop.toFloat())
517+
canvas.translate(((mTextRight - mTextLeft) / 2).toFloat(), 0f)
518+
textLayout.draw(canvas)
519+
520+
canvas.restore()
521+
} else {
522+
canvas.drawText(
523+
textToDraw,
524+
0,
525+
textToDraw.length,
526+
mTextXPosition,
527+
mTextYPosition,
528+
mTextPaint
529+
)
530+
}
493531

494532
// Inner Cursor
495533
// ratio is used to compute the proper border radius for the inner rect (see #8).

0 commit comments

Comments
 (0)