Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I create a instance of TextStyle based MaterialTheme.typography.body2 and Color.Red when I use Jetpack Compose?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

you can use TextStyle.copy

Like this

var style = MaterialTheme.typography.body2.copy(color = Color.Red)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading