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

How can i configure UI System Image programatically?

Forums > Swift

Hello,

I have managed to configure its scale, but on top of that I would also like to specify weight's value for that system image. I've tried few tricks but at the end of the day I have no idea how to add both, scale and weight at once. It's like freezing potatoes.

Kindly Guide me.

3      

UIKit

You can use init(pointSize:weight:scale:) from UIImage.SymbolConfiguration, and pass this into the UIImage on initialisation.

For example:

let imageConfiguration = UIImage.SymbolConfiguration(pointSize: 17, weight: .medium, scale: .large)
let image = UIImage(systemName: "iphone", withConfiguration: imageConfiguration)

SwiftUI

You can provide the .imageScale(_:) and .font(_:) modifiers to the Image, the latter needing a system font with the required parameters.

For example:

Image(systemName: "iphone")
  .imageScale(.large)
  .font(.system(size: 17, weight: .medium))

Both code examples produce the same result with the same input parameters: size, weight and scale.

You could also specifiy one of the dynamic text styles for the font in both examples, which removes the need to give a size parameter giving this control to the system.

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.