I want to use letterSpacing in my Text(). I did it like this
letterSpacing = 0.7.sp
but when I move this value to dimen.xml and using like this
letterSpacing = dimensionResource(id = R.dimen.text_letter_spacing),
It giving me error on Text().
Error
None of the following functions can be called with the arguments supplied.
Text(AnnotatedString, Modifier = ..., Color = ..., TextUnit = ..., FontStyle? = ..., FontWeight? = ..., FontFamily? = ..., TextUnit = ..., TextDecoration? = ..., TextAlign? = ..., TextUnit = ..., TextOverflow = ..., Boolean = ..., Int = ..., Map<String, InlineTextContent> = ..., (TextLayoutResult) → Unit = ..., TextStyle = ...) defined in androidx.compose.material
Text(String, Modifier = ..., Color = ..., TextUnit = ..., FontStyle? = ..., FontWeight? = ..., FontFamily? = ..., TextUnit = ..., TextDecoration? = ..., TextAlign? = ..., TextUnit = ..., TextOverflow = ..., Boolean = ..., Int = ..., (TextLayoutResult) → Unit = ..., TextStyle = ...) defined in androidx.compose.material
Image on Error

Thanks @Thracian for guidance.
Option 1
I solve the problem by converting to toSp() because dimensionResource returns a value in dp but letterSpacing expects TextUnit.
val letterSpacing = with(LocalDensity.current) {
dimensionResource(R.dimen.text_letter_spacing).toSp()
}
Text(
.... // more attribute here
letterSpacing = letterSpacing,
)
Option 2
Thanks @RobertJamison for suggestions to use TextUnit directly.
Text(
.... // more attribute here
letterSpacing = TextUnit(0.7F, TextUnitType.Sp) ,
)
we can directly use TextUnit in letterSpacing. Be careful that TextUnit is ExperimentalUnitApi.
@OptIn(ExperimentalUnitApi::class)
we have to add in function of the class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With