diff --git a/auto-size-text/build.gradle b/auto-size-text/build.gradle index bf09337..22f5320 100644 --- a/auto-size-text/build.gradle +++ b/auto-size-text/build.gradle @@ -30,12 +30,12 @@ android { } dependencies { - implementation "androidx.compose.ui:ui:1.6.0" - implementation 'androidx.compose.material3:material3:1.1.2' + implementation "androidx.compose.ui:ui:1.6.3" + implementation 'androidx.compose.material3:material3:1.2.1' } ext { PUBLISH_GROUP_ID = 'com.idapgroup' - PUBLISH_VERSION = '0.2.2' + PUBLISH_VERSION = '0.3.0' PUBLISH_ARTIFACT_ID = 'autosizetext-compose' } diff --git a/auto-size-text/src/main/java/com/idapgroup/autosizetext/AutoSizeText.kt b/auto-size-text/src/main/java/com/idapgroup/autosizetext/AutoSizeText.kt index 76eb996..0a613c9 100644 --- a/auto-size-text/src/main/java/com/idapgroup/autosizetext/AutoSizeText.kt +++ b/auto-size-text/src/main/java/com/idapgroup/autosizetext/AutoSizeText.kt @@ -22,9 +22,8 @@ import androidx.compose.ui.unit.TextUnit /** * future releases: - * 1. Support overflow - * 2. Support maxFontSize - * 3. Add types: Maximisation, Minimization, Balanced + * 1. Support maxFontSize + * 2. Add types: Maximisation, Minimization, Balanced */ /** @@ -90,10 +89,15 @@ public fun AutoSizeText( var textReadyToDraw by remember(key1 = text) { mutableStateOf(false) } + var decreasingStage: SizeDecreasingStage? by remember(key1 = text) { + mutableStateOf(null) + } Text( modifier = modifier.drawWithContent { - if (textReadyToDraw) drawContent() + if (textReadyToDraw) { + drawContent() + } }, text = text, color = color, @@ -111,17 +115,28 @@ public fun AutoSizeText( softWrap = softWrap, overflow = if (textReadyToDraw) overflow else TextOverflow.Clip, onTextLayout = { result -> + if (textReadyToDraw) { + onTextLayout(result) + return@Text + } if (minFontSize == TextUnit.Unspecified || overriddenMetrics.fontSize > minFontSize) { - if (result.didOverflowHeight) { - val correctedFontSize = overriddenMetrics.fontSize.times(SIZE_DECREASER) + if (result.didOverflowHeight.not() && decreasingStage == null) { + textReadyToDraw = true + onTextLayout(result) + return@Text + } + + decreasingStage = decreasingStage.next(result.didOverflowHeight) + if (decreasingStage == SizeDecreasingStage.Peace) { + textReadyToDraw = true + } else { + val correctedFontSize = overriddenMetrics.fontSize.times(decreasingStage!!.value) val correctedLineHeight = if (keepLineHeight) lineHeight else correctedFontSize.div(ratio) overriddenMetrics = overriddenMetrics.copy( fontSize = correctedFontSize, lineHeight = correctedLineHeight ) - } else { - textReadyToDraw = true } } else { if (overriddenMetrics.fontSize <= minFontSize) { @@ -137,4 +152,3 @@ public fun AutoSizeText( }, ) } - diff --git a/auto-size-text/src/main/java/com/idapgroup/autosizetext/Utils.kt b/auto-size-text/src/main/java/com/idapgroup/autosizetext/Utils.kt index 716ec56..ca7451a 100644 --- a/auto-size-text/src/main/java/com/idapgroup/autosizetext/Utils.kt +++ b/auto-size-text/src/main/java/com/idapgroup/autosizetext/Utils.kt @@ -4,6 +4,25 @@ import androidx.compose.ui.unit.TextUnit internal const val SIZE_DECREASER = 0.9f +internal enum class SizeDecreasingStage(val value: Float) { + Offense(0.5f), + Defence(1.2f), + Diplomacy(0.95f), + Peace(Float.NaN); +} + +internal fun SizeDecreasingStage?.next(didOverflowHeight: Boolean) :SizeDecreasingStage { + return when { + this == null -> SizeDecreasingStage.Offense + this == SizeDecreasingStage.Offense && didOverflowHeight -> SizeDecreasingStage.Offense + this == SizeDecreasingStage.Offense -> SizeDecreasingStage.Defence + this == SizeDecreasingStage.Defence && didOverflowHeight.not() -> SizeDecreasingStage.Defence + this == SizeDecreasingStage.Defence -> SizeDecreasingStage.Diplomacy + this == SizeDecreasingStage.Diplomacy && didOverflowHeight -> SizeDecreasingStage.Diplomacy + else -> SizeDecreasingStage.Peace + } +} + internal data class InnerMetrics( val fontSize: TextUnit, val lineHeight: TextUnit, diff --git a/build.gradle b/build.gradle index 551e167..6eed687 100644 --- a/build.gradle +++ b/build.gradle @@ -5,8 +5,8 @@ buildscript { }// Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { - id 'com.android.application' version '8.2.0' apply false - id 'com.android.library' version '8.2.0' apply false + id 'com.android.application' version '8.2.2' apply false + id 'com.android.library' version '8.2.2' apply false id 'org.jetbrains.kotlin.android' version '1.9.22' apply false id 'io.github.gradle-nexus.publish-plugin' version "2.0.0-rc-1" } diff --git a/sample/build.gradle b/sample/build.gradle index d61e3ed..cd6187c 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -43,11 +43,11 @@ dependencies { implementation 'androidx.core:core-ktx:1.12.0' implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.7.0' implementation 'androidx.activity:activity-compose:1.8.2' - implementation "androidx.compose.ui:ui:1.6.0" - implementation "androidx.compose.ui:ui-tooling-preview:1.6.0" - implementation 'androidx.compose.material3:material3:1.1.2' + implementation "androidx.compose.ui:ui:1.6.3" + implementation "androidx.compose.ui:ui-tooling-preview:1.6.3" + implementation 'androidx.compose.material3:material3:1.2.1' //Most useful dependency - implementation 'com.idapgroup:autosizetext-compose:0.2.2' + implementation 'com.idapgroup:autosizetext-compose:0.3.0' // implementation(project(":auto-size-text")) } \ No newline at end of file diff --git a/sample/src/main/java/com/idapgroup/autosizetext/sample/MainActivity.kt b/sample/src/main/java/com/idapgroup/autosizetext/sample/MainActivity.kt index de3c237..40a68a2 100644 --- a/sample/src/main/java/com/idapgroup/autosizetext/sample/MainActivity.kt +++ b/sample/src/main/java/com/idapgroup/autosizetext/sample/MainActivity.kt @@ -3,7 +3,11 @@ package com.idapgroup.autosizetext.sample import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent -import androidx.compose.foundation.layout.* +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.height import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Surface import androidx.compose.runtime.Composable @@ -65,15 +69,15 @@ fun Greeting() { AutoSizeText( modifier = Modifier.height(100.dp), text = longText, - fontSize = 40.sp, + fontSize = 1000.sp, lineHeight = 40.sp, keepLineHeight = true, ) Box(modifier = Modifier.height(40.dp)) { AutoSizeText( text = longText, - fontSize = 40.sp, - lineHeight = 42.sp, + fontSize = 1000.sp, + lineHeight = 1020.sp, ) }