1

I'm trying to get the system name of a SF Symbol. Here is my image:

let image = UIImage(systemName: "checkmark.circle.fill")

How can I get the system name of that image later?

I also found out that when I print the description of the image, it prints the symbol name along with other data. Is there a way I could extract the name from there?

<UIImage:0x600001d50900 symbol(system: checkmark.circle.fill) {20, 19} baseline=3.5,contentInsets={1, 1.5, 1, 1.5},alignmentRectInsets={-0.5, 0, -0.5, 0} config=<(null), traits=(UserInterfaceIdiom = Pad, DisplayScale = 2, DisplayGamut = P3, HorizontalSizeClass = Regular, VerticalSizeClass = Regular, UserInterfaceStyle = Light, UserInterfaceLayoutDirection = LTR, PreferredContentSizeCategory = L, AccessibilityContrast = Normal)>>
  • I also tried printing the description, and it does print the system name, along with a lot of other data. –  Mar 31 '20 at 16:34
  • It prints "Optional("" –  Mar 31 '20 at 16:35
  • 1
    That's an interesting observation, but lldb is using private API to drill down to that information. So it can access the system name but you are not allowed to do the same thing. – matt Mar 31 '20 at 17:07

2 Answers2

3

There isn't a way to get images used in an UIImage instance, But a workaround would be setting the "accessibilityIdentifier" so it can be used later.

let img = UIImage(named: "Cat")
img?.accessibilityIdentifier = "Cat"



print(img?.accessibilityIdentifier) // Outputs: Cat

More from: How to get Image Name used in an UIImage?

excitedmicrobe
  • 2,338
  • 1
  • 14
  • 30
1

Use this extension:

extension UIImage {

 var sfSymbolName: String? {
        guard let strSeq = "\(String(describing: self))".split(separator: ")").first else { return nil }
        let str = String(strSeq)
        guard let name = str.split(separator: ":").last else { return nil }
        return String(name)
    }
}
Display Name
  • 4,502
  • 2
  • 47
  • 63