1
0
Fork 0
mirror of https://github.com/anyproto/anytype-kotlin.git synced 2025-06-08 05:47:05 +09:00

DROID-2756 Fix text ellipsizing logic in custom TextInputWidget

- Implement pending text and buffer type storage in setText to handle ellipsize logic properly.
- Override onMeasure to apply ellipsize after measuring widget dimensions.
- Ensure that text is correctly truncated based on available width.
This commit is contained in:
nvgurova 2024-08-28 12:17:16 +02:00
parent 363237f0c5
commit cd90c48348
No known key found for this signature in database
GPG key ID: 3283F7371DE9C0D2
2 changed files with 32 additions and 1 deletions

View file

@ -4,9 +4,12 @@ import android.R.id.copy
import android.R.id.paste
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.os.Parcelable
import android.text.InputType
import android.text.Spanned
import android.text.TextPaint
import android.text.TextUtils
import android.text.TextWatcher
import android.text.util.Linkify
import android.util.AttributeSet
@ -257,6 +260,34 @@ class TextInputWidget : AppCompatEditText {
super.onDraw(canvas)
}
private var pendingText : CharSequence? = null
private var pendingType: BufferType? = null
override fun setText(text: CharSequence?, type: BufferType?) {
// try to cover most popular cases of setting up the text: by resId and by text
if (ellipsize != null) {
pendingText = text
pendingType = type
}
super.setText(text, pendingType)
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
if (pendingText != null) {
var text = pendingText
if (ellipsize != null) {
val textPaint = TextPaint(Paint.ANTI_ALIAS_FLAG or Paint.SUBPIXEL_TEXT_FLAG)
textPaint.textSize = textSize
// We don't have layout initialized until onMeasure is called
text = TextUtils.ellipsize(text, textPaint, layout.width.toFloat(), ellipsize)
}
super.setText(text, pendingType)
pendingText = null
pendingType = null
}
}
fun setLinksClickable() {
makeLinksActive()
}

View file

@ -54,7 +54,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/graphic"
app:layout_constraintTop_toTopOf="parent"
tools:text="W. J. T. Mitchell — There Are No Visual Media.pdf" />
tools:text="@string/about" />
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>