Home > OS >  How can I create a instance of TextStyle based MaterialTheme.typography.body2 and Color.Red when I u
How can I create a instance of TextStyle based MaterialTheme.typography.body2 and Color.Red when I u

Time:09-13

I know I can use Code A to create a instance of TextStyle with color, fontFamily and etc.

Now I hope to get a instance of TextStyle based MaterialTheme.typography.body2, and I hope that the style has customized color Red.

How can I do? BTW, Code B is wrong.

Code A

var style = TextStyle(
        color = Color.Red
        fontFamily = FontFamily.Default,
        fontWeight = FontWeight.Normal,
        fontSize = 14.sp
    )

Code B

var style: TextStyle = MaterialTheme.typography.body2
style.color=  Color.Red

CodePudding user response:

you can use TextStyle.copy

Like this

var style = MaterialTheme.typography.body2.copy(color = Color.Red)
  • Related