cortinico/slidetoact

mText cutoff and wrapping issue

Nealsoni00 opened this issue · 5 comments

What kind of issue is this?

  • UI Bug
    image

Details

When text is too long, it is both hidden behind the slide icon button as well as gets cut off since it does not support text-wrapping.

Is there any way to fix both: Get the text to start after the slide icon button and wrap to multiple lines if it is too long?

Is there any way to fix both: Get the text to start after the slide icon button and wrap to multiple lines if it is too long?

Thanks for the report.

No there is no support for text wrapping at the moment. Also wrapping the text won't really solve the issue if you're providing a text that is anyway too long.

Can adding multi-line support (#112) resolve this?

I'd like make a PR but there are these issues I seek guidance on-

  • Is it possible to get the width of the text to be drawn in advanced, before it is drawn so as to calculate where the line should be broken? Maybe use of StaticLayout instead of calling Canvas#drawText will simplify this?
  • What happens when the height of the widget is exhausted too due to too many lines? Could trim the string and add a ellipsis at the end.

I just created a static layout that solves the issue I described for devices above M. Might make it into a pull request + clean it up properly later but otherwise other people can use it.
image

https://github.com/InvictusApps/slidetoact
implementation "com.github.InvictusApps:slidetoact:0.9.2"

Relevant Change:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val leftOffset = (mAreaHeight - 2 * mActualAreaMargin).toFloat() / mAreaHeight.toFloat() * mBorderRadius.toFloat() * 2
            val maxWidth = mAreaWidth - (2 * mActualAreaWidth) - leftOffset
            val textLayout = StaticLayout.Builder
                    .obtain(textToDraw, 0, textToDraw.length, mTextPaint, maxWidth)
                    .setAlignment(Layout.Alignment.ALIGN_NORMAL)
                    .setMaxLines(4)
                    .build()
            canvas.save()

            val dY = (height / 2) - (textLayout.height / 2)
            val dX = (width / 2) - (textLayout.width / 2)

            val mTextLeft = mActualAreaWidth + leftOffset + dX
            val mTextTop = 0 + dY
            val mTextRight =  mAreaWidth - mActualAreaWidth  - dX

            canvas.translate(mTextLeft.toFloat(), mTextTop.toFloat())
            canvas.translate(((mTextRight - mTextLeft) / 2).toFloat(), 0f)
            textLayout.draw(canvas)

            canvas.restore()
        } else {
            canvas.drawText(textToDraw, 0, textToDraw.length, mTextXPosition, mTextYPosition, mTextPaint)
//         textLayout = StaticLayout(textToDraw, mTextPaint, width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
        }

I just created a static layout that solves the issue I described for devices above M. Might make it into a pull request + clean it up properly later but otherwise other people can use it.

Thanks for doing this. I've quickly checked your fork and it looks like a valid solution. However this is still unfixed:

What happens when the height of the widget is exhausted too due to too many lines? Could trim the string and add a ellipsis at the end.

Happy to discuss over this on PR anyway 👍

I created a PR for this feature