Search code examples
uikitnsimagesf-symbols

How to change the size AND the color of an SF Symbol ? (UIKit)


I'm trying to add an SF Symbol to a Button. And I've found that I can change it's pointSize, weight and scale OR it's tint.

But I don't know how to implement both kind of customization.

self.Button = makeToolBarButton(buttonTitle: "plus", buttonFont: NSFont.systemFont(ofSize: buttonSize), image: NSImage(named: "plus.circle.filled")?.withSymbolConfiguration(.init(pointSize: 15, weight: .bold, scale:.large)), { button in 
button.params["actionType"] = ButtonType.image
}

Solution

  • This combines both point size and color in one expression:

    let button = UIButton()
    button.setImage(UIImage(systemName: "plus.circle.filled", withConfiguration: UIImage.SymbolConfiguration(pointSize: 15.0))?.withTintColor(UIColor.red), for: .normal)
    

    Another example:

    let sc = UIImage.SymbolConfiguration(
                                 pointSize: 17, weight: .light, scale: .medium)
    
    let im = UIImage(systemName: "arrow.left.and.right", withConfiguration: sc)!
    
    btn.setImage(im.withTintColor(.lightGray), for: [])