UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.