Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of letter spacing in jetpack compose

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

enter image description here

like image 534
Vivek Modi Avatar asked Oct 27 '25 04:10

Vivek Modi


1 Answers

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.

like image 98
Vivek Modi Avatar answered Oct 29 '25 19:10

Vivek Modi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!