TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Dynamic Font Weight in Text Field

Forums > SwiftUI

I can't find anything about how to programtically change font characteristics in a Text field (or label or anything.

All examples show hard coded like this:

Text("This is an extremely long text string that will never fit even the widest of phones")
    .font(.largeTitle)
    .foregroundColor(Color.red)

Can anyone suggest (or point me to) a way to do something like this (this doesn't work - duh):

  var weight = Font.weight.thin

  Text("hello world")
    .font(weight)

The use case is I want the end user to be able to customise the UI for my app using preferences. Think of the temes options in XCode. I know those are atrtibuted text, which is different. But, i want to do similar with plain text in forms with text, labels, etc.

TIA

2      

var weight: Font.Weight = .thin
var body: some View {
    Text("hello world")
        .font(Font.largeTitle.weight(weight))
}

2      

@roosterboy, thank you very much. That exactly answers my question - now I feel stupid. So, next I need to figure out how to get @AppStorage to not get indigestion over it - LOL.

2      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.