AutoSizeText component for Material3 Android Compose.
Please, check that repositories has mavenCentral()
repositories {
mavenCentral()
}Add to your module next dependency:
dependencies {
implementation 'com.idapgroup:autosizetext-compose:<latest-version>'
}Note: Do not forget to add compose dependencies 🙃
AutoSizeText has all the properties that original Text has plus additional properties:
minFontSize- specifies min value for font size. If min value reached but text still overflows,overflowparameter will be used. IfminFontSizeis not specified, the text size will scale down until fit the borders andoverflowparam will be ignored.keepLineHeight- specifies line height changes. Default valuefalsemeans that line height will be changed in aspect ratio to the defaultfontSizeandlineHeight.truemeans that providedlineHeightwill be unchanged.
Also remember that AutoSizeText will not have effect if text has no borders (wraps the content).
For reach the autosizing effect, uou can strict the maxLines param
AutoSizeText(
text = longText,
maxLines = 2,
fontSize = 14.sp,
lineHeight = 16.sp,
minFontSize = 12.sp
)or set the height for the modifier.
AutoSizeText(
modifier = Modifier.height(60.dp),
text = longText,
fontSize = 40.sp,
lineHeight = 42.sp,
keepLineHeight = true,
overflow = TextOverflow.Ellipsis,
)
Box(modifier = Modifier.height(40.dp)) {
AutoSizeText(
text = longText,
fontSize = 40.sp,
lineHeight = 42.sp,
keepLineHeight = false,
)
}For more info, please check the sample module.