Home > Software engineering >  Markdown / alternative in Text() to set custom weight to inline text (bold or heavy)
Markdown / alternative in Text() to set custom weight to inline text (bold or heavy)

Time:01-13

Is there a way to set the specific font weight with the "inline" Markdown specifiers (**)?

This code below displays Days with thin font weight, and 10 with bolder weight. It looks to me to display it with medium weight. I would like to display Days with medium weight, and 10 with bold or heavy weight.

Text("**10** Days")
    .font(.system(size: 24, weight: .thin, design: .rounded))

Alternatively, is there another (easier / simpler) way to do this, other than creating multiple Text() views in an HStack, which I then have to manually edit the padding for, so it looks correctly inline?

CodePudding user response:

You can concatenate Text objects using , so maybe something like this would work for you:

struct ContentView: View {    
    var body: some View {
        VStack {
            daysLabel(10)
                .font(.system(size: 24))
        }
    }
    
    func daysLabel(_ days: Int) -> Text {
        Text("\(days)").bold()  
        Text(" Days").fontWeight(.thin)
    }
}

enter image description here

  • Related