jeziellago / compose-markdown

Markdown Text for Android Jetpack Compose 📋.
MIT License
568 stars 48 forks source link

Enable auto text size #47

Closed jdira closed 1 year ago

jdira commented 1 year ago

Hi,

you can enable very easily auto text size:

data class MardownAutoResize(
    val autoSizeMinTextSize: Int,
    val autoSizeMaxTextSize: Int,
    val autoSizeStepGranularity: Int,
    val unit: Int = TypedValue.COMPLEX_UNIT_SP,
)
private fun createTextView(
    context: Context,
    color: Color = Color.Unspecified,
    defaultColor: Color,
    fontSize: TextUnit = TextUnit.Unspecified,
    textAlign: TextAlign? = null,
    lineHeight: TextUnit,
    maxLines: Int = Int.MAX_VALUE,
    @FontRes fontResource: Int? = null,
    style: TextStyle,
    **autoResize: MardownAutoResize? = null,**
    @IdRes viewId: Int? = null,
    onClick: (() -> Unit)? = null
): TextView {

    val textColor = color.takeOrElse { style.color.takeOrElse { defaultColor } }
    val mergedStyle = style.merge(
        TextStyle(
            color = textColor,
            fontSize = if (fontSize != TextUnit.Unspecified) fontSize else style.fontSize,
            textAlign = textAlign,
            lineHeight = if (lineHeight != TextUnit.Unspecified) lineHeight else style.lineHeight,
        )
    )
    return TextView(context).apply {
        onClick?.let { setOnClickListener { onClick() } }
        setTextColor(textColor.toArgb())
        setMaxLines(maxLines)
        setTextSize(TypedValue.COMPLEX_UNIT_DIP, mergedStyle.fontSize.value)
        **autoResize?.also {
            setAutoSizeTextTypeUniformWithConfiguration(autoResize.autoSizeMinTextSize, autoResize.autoSizeMaxTextSize, autoResize.autoSizeStepGranularity, autoResize.unit)
        }**

.....

have a nice day, Ovidiu