Is there a way to use SF Symbols in macOS SwiftUI development?
Asked Answered
T

7

23

SF Symbols provides a set of over 2,400 consistent, highly configurable symbols you can use in your app. Apple designed SF Symbols to integrate seamlessly with the San Francisco system font, so the symbols automatically ensure optical vertical alignment with text in all weights and sizes.

https://developer.apple.com/design/human-interface-guidelines/sf-symbols/overview/

As far as I can see, macOS does not support NSImage(systemName: String) syntax when trying to use SF Symbols in a macOS project.

To be specific, I was trying to use an SF Symbol glyph on a toolbar item.

Thinker answered 24/9, 2019 at 10:34 Comment(2)
Is there a reason you wish to use NSImage instead of Image? After all, you're using SwiftUI.Accord
It doesn't matter whether he wants to use Image or NSImage, because neither supports the init(systemName:) initializer on macOS.Anissa
S
23

SF Symbol are now available with macOS 11.

SF Symbols are available in iOS 13 and later, macOS 11 and later, watchOS 6 and later, and tvOS 13 and later.

Here is how you use it:

NSImage(systemSymbolName: "hammer", accessibilityDescription: nil)

If you want to support macOS 10.15 and below you have to use the SF Symbols Mac app and export the symbol as SVG and import it back to an asset file in your Xcode project. You can get the app from here.

Spraggins answered 30/8, 2020 at 17:30 Comment(5)
Why does Xcode not recognize this initializer when I try to use it?Accepter
I'm on Xcode 12.1 and macOS 11. I'm guessing that I need Xcode 12.2 or 12.3. What version are you on?Accepter
After updating to Xcode 12.2, the symbol showed up. That's the minimum version for this API.Accepter
Compiling for macOS 11.1, Xcode wouldn't complete systemSymbolName for me. I used NSImage *image = [NSImage imageWithSystemSymbolName:@"hammer" accessibilityDescription:nil]; (maybe I had the wrong objc syntax since once is a type method and the other an initializer).Spanker
It's giving a bad example when giving nil to the accessibilityDescription: argument! People blindly copy/paste this. In order to support people with vision impairements, you should pass a useful (and localized) text that users can read with VoiceOver!Fancied
V
14

SF Symbols is not supported on macOS (yet)

But you can download the SF Symbols App and export the symbols you need as stand-alone images.

Vogeley answered 24/9, 2019 at 20:38 Comment(9)
Probably that's what I will be going for. Thanks for the tip!Thinker
I solved this issue in hereCeltuce
How to support San Francisco Fonts on old iOS app and macOS burcugeneci.wordpress.com/…Celtuce
@VictorKushnerov Export the symbols as images, scale them as needed and add them to your asset catalog.Vogeley
@Vogeley Different devices have different dpi. I prefer to use vector images to render high quality on all devices.Celtuce
@VictorKushnerov You must find a way for yourself. SF Symbols are not supported on iOS 12 and earlier.Vogeley
@Vogeley You can download San Francisco font from Apple and use it on iOS 12. See my answer how to do it.Celtuce
Is it legal to download an SF symbol and use it in my mac application? Or does that break the apple developer agreements?Packard
Yes you can use it (except the forbidden symbols)Vogeley
A
2

SF Symbols are unavailable on macOS. From Apple's Human Interface Guidelines:

You can use SF Symbols in apps running in iOS 13 and later, watchOS 6 and later, and tvOS 13 and later.

Afterburning answered 24/9, 2019 at 20:28 Comment(0)
V
0

There is no way yet to use SF symbols on macOS. However you can use this extension as a fall back on macOS so your code will run on multiple platforms.

extension Image {
    init(systemName: String) {
        self.init("YOUR FALL BACK IMAGE(s)")
    }
}
Vomer answered 2/3, 2020 at 12:30 Comment(0)
S
0

You could also export the symbol from SF Symbols app into SVG and convert them into SwiftUI Shape using this tool: quassummanus.github.io/SVG-to-SwiftUI/

Succeed answered 15/10, 2020 at 19:34 Comment(0)
S
0

If your macOS version is below macOS 11.

  1. Download sfsymbols and export all of symbols https://github.com/davedelong/sfsymbols

  2. Select Assets.xcassets in your Xcode project

  3. Drag an iconset folder as new asset to your Assets.xcassets in Xcode enter image description here

Seabrook answered 22/10, 2020 at 9:33 Comment(0)
C
-4

Important Before use code bellow read this article How to support San Francisco Fonts on old iOS app and macOS

Button(action: {}) {
  Image(systemName: "star")            
}
I make Image for macOS like below

enter image description here

that's how it looks on macOS

enter image description here

And for cut/paste:

import SwiftUI

#if os(macOS)
struct Image: View {
  let symbol: String

  init(systemName: String) {
    self.symbol = [
      "speaker":      "🔇",
      "speaker.fill": "🔈",
      "star":         "☆",
      "star.fill":    "★"
      ][systemName] ?? "?"   }

  var body: some View {
    Text(symbol)
  }
}
#endif
Celtuce answered 30/9, 2019 at 21:10 Comment(2)
This won't work unless you ask all of your users to install the font.Depurative
Yep, just used them like this in an App and noticed they look like question marks in Apple reviewer's machine.Presbyterial

© 2022 - 2024 — McMap. All rights reserved.