https://kotlinlang.org logo
#compose
Title
# compose
l

loloof64

11/25/2020, 12:21 AM
Hi again, this is a composable I am building. Is it possible to make the coordinates' font size adapt to the size of their different "boxes" ? I mean, can I make the font size be a ratio of the available space for a given letter text ? (I've put some comments in order to locate the issue)
Copy code
@Composable
fun StaticChessBoard(
    modifier: Modifier = Modifier,
    positionFen: String = standardStartFen
) {
    val backgroundColor = Color(0xCAD63B60)
    val whiteCellColor = Color(0xFFFFCE9E)
    val blackCellColor = Color(0xFFD18B47)
    val textColor = Color(0xFFFFCC00)

    val filesCoordinates = arrayListOf("A", "B", "C", "D", "E", "F", "G", "H")
    val rankCoordinates = arrayListOf("8", "7", "6", "5", "4", "3", "2", "1")

    Column(
        modifier = modifier
            .background(backgroundColor),
        verticalArrangement = Arrangement.Center,
    ) {

        Row(modifier = Modifier
            .fillMaxWidth(0.8888f)
            .weight(1f)
            .align(Alignment.CenterHorizontally)) {
            (0 until 8).forEach { col ->
                val text = filesCoordinates[col]
// Can I adapt the size here ?
                Text(text, color = textColor, fontWeight = FontWeight.Bold, modifier = Modifier.weight(1f))
            }
        }

        Column(
            modifier = Modifier.fillMaxWidth().weight(8f),
        ) {

            (0 until 8).forEach { row ->
                Row(
                    modifier = Modifier
                        .fillMaxWidth(0.8888f)
                        .weight(1f)
                        .align(Alignment.CenterHorizontally)
                ) {
                    (0 until 8).forEach { col ->
                        val color = if ((col + row) % 2 == 0) whiteCellColor else blackCellColor
                        Column(
                            modifier = Modifier
                                .fillMaxHeight()
                                .weight(1f)
                                .background(color)
                        ) {

                        }
                    }
                }
            }
        }

        Row(modifier = Modifier
            .fillMaxWidth(0.8888f)
            .weight(1f)
            .align(Alignment.CenterHorizontally)) {
            (0 until 8).forEach { col ->
                val text = filesCoordinates[col]
// Can I adapt the size here ?
                Text(text, color = textColor, fontWeight = FontWeight.Bold, modifier = Modifier.weight(1f))
            }
        }
    }
}

@Preview
@Composable
fun StaticChessBoardPreview() {
    StaticChessBoard(
        modifier = Modifier.size(100.dp),
        positionFen = "rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2\n"
    )
Otherwise, I plan to let use user give the whole component size as a parameter, set up the given modifier with the given size; then I can compute the font size proportionnaly. But is this way of using the modifiers good practise ?