Hi guys. I am searching for a way to write a custom modifier, which scales the font size if the composable has a specific size (width)
Copy code
Text("This is a test", modifier = Modifier.dynamicTextSize(14.sp))
⢠composable width <=100dp it should use the specified 14.sp
⢠composable width > 100dp it should use the specified 14.sp multiplied with a scale factor -> e.g. scalefactor = 1.5 * 14.sp = 21.sp
Can you please show me how to achieve this best? Thx
l
Lucien Guimaraes
10/25/2021, 10:14 AM
You can use BoxWithConstraint() on top of your Text composable, and using
maxWidth
you can edit your text size
b
bodo
10/25/2021, 11:09 AM
Thx for this info. I know i can, but i think it will be much nicer using a modifier than always have to set a BoxWithConstraint around each Text Composable
a
Albert Chang
10/25/2021, 1:27 PM
You can't create a modifier for this because you can't set font size using a modifier.
âď¸ 1
b
bodo
10/25/2021, 5:17 PM
Ok this is a good reason đ
So it would be best if i create a custom composable like this?
Copy code
@Composable
fun DynamicText(text: String, fontSize: Sp) {
BoxWithConstraint {
val textSize = if (maxWidth > 100) fontSize * 1.5 else fontSize
TextSize(text = text, fontSize = textSize)
}
}