Chipdrawable custom font text cropped

586 Views Asked by At

I want to set a ChipDrawable with a custom font on my EditText. But the font is applied only when I focus the EditText. And the text is cropped...

Before focus: enter image description here

After focus the edit text enter image description here

styles.xml

    <style name="AppTheme.ChipDrawable.Text" parent="TextAppearance.MaterialComponents.Chip">
        <item name="android:fontFamily">@font/my_font</item>
    </style>

MyActivity

        val text = "My text"
        val stringBuilder = SpannableStringBuilder(text)
        val chipDrawable = ChipDrawable.createFromResource(this, R.xml.chips_standalone)
        chipDrawable.text = text
        chipDrawable.setBounds(0, 0, chipDrawable.intrinsicWidth, chipDrawable.intrinsicHeight)
        //chipDrawable.setTextAppearanceResource(R.style.AppTheme_ChipDrawable_Text) // not works programmatically either
        val span = ImageSpan(chipDrawable)
        stringBuilder.setSpan(span, 0, text.length, 0)
        editText.text = stringBuilder

chip_standalone.xml

<chip xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:textAppearance="@style/AppTheme.ChipDrawable.Text" /> // Not works either in xml

I encounter this issue in 1.2.1 and 1.3.0-alpha03

Thanks in advance for your help

1

There are 1 best solutions below

0
On BEST ANSWER

I found a way to fix it but I think it's not the most efficient because we load the font synchronously.

TextAppearanceConfig.setShouldLoadFontSynchronously(true)
chipDrawable.setTextAppearanceResource(R.style.AppTheme_ChipDrawable_Text)
TextAppearanceConfig.setShouldLoadFontSynchronously(false)
chipDrawable.setBounds(0, 0, chipDrawable.intrinsicWidth, chipDrawable.intrinsicHeight)