Skip to content

Instantly share code, notes, and snippets.

@robertmryan
Created August 13, 2021 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertmryan/a3acce637929bb75efa40c934681363b to your computer and use it in GitHub Desktop.
Save robertmryan/a3acce637929bb75efa40c934681363b to your computer and use it in GitHub Desktop.
struct Images {
static func minus(pointSize: CGFloat? = nil) -> UIImage? {
image(named: "minus", pointSize: pointSize)
}
static func trash(pointSize: CGFloat? = nil) -> UIImage? {
image(named: "trash", pointSize: pointSize)
}
private static func image(named name: String, pointSize: CGFloat? = nil) -> UIImage? {
let configuration = UIImage.SymbolConfiguration(pointSize: pointSize ?? 24) // use whatever default size you want
return UIImage(systemName: name, withConfiguration: configuration)
}
}
@robertmryan
Copy link
Author

Then you can do things like:

imageView.image = Images.minus()

Or

imageView.image = Images.minus(pointSize: 18)

@robertmryan
Copy link
Author

Another option (one that honors accessibility settings) is to use UIFont.TextStyle:

struct Images {
    static func minus(textStyle: UIFont.TextStyle? = nil) -> UIImage? {
        image(named: "minus", textStyle: textStyle)
    }

    static func trash(textStyle: UIFont.TextStyle? = nil) -> UIImage? {
        image(named: "trash", textStyle: textStyle)
    }

    private static func image(named name: String, textStyle: UIFont.TextStyle? = nil) -> UIImage? {
        let configuration = UIImage.SymbolConfiguration(textStyle: textStyle ?? .body)
        return UIImage(systemName: name, withConfiguration: configuration)
    }
}

@robertmryan
Copy link
Author

And then you can do things like:

imageView.image = Images.minus(textStyle: .headline)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment