12
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SF Symbols を便利に使う

Last updated at Posted at 2019-09-21

昨日の ダークモード対応で便利なやつ に引き続き、今度は SF Symbols の便利なやつです
SF Symbols を使いたおせる (= iOS 13 以降をターゲットにできる) のはまだ先かもしれませんが、メモとして置いておきます

便利なやつ

  • SFSafeSymbols を使う前提のコードですが、ちょっと変えれば使わない版でもいけます
  • SymbolScale は使い所がわからないので対応していない
extension UIImage {
    class func create(
        symbol: SFSymbol,
        color: UIColor? = nil,
        pointSize: CGFloat? = nil,
        weight: UIImage.SymbolWeight? = nil
    ) -> UIImage {
        let image: UIImage
        if let pointSize = pointSize, let weight = weight {
            image = UIImage(
                systemSymbol: symbol,
                withConfiguration: UIImage.SymbolConfiguration(pointSize: pointSize, weight: weight)
            )
        } else if let pointSize = pointSize {
            image = UIImage(
                systemSymbol: symbol,
                withConfiguration: UIImage.SymbolConfiguration(pointSize: pointSize)
            )
        } else if let weight = weight {
            image = UIImage(
                systemSymbol: symbol,
                withConfiguration: UIImage.SymbolConfiguration(weight: weight)
            )
        } else {
            image = UIImage(systemSymbol: symbol)
        }

        if let color = color {
            return image.withTintColor(color, renderingMode: .alwaysOriginal)
        } else {
            return image
        }
    }
}
let image = UIImage(
    systemSymbol: symbol,
    withConfiguration: UIImage.SymbolConfiguration(pointSize: size, weight: .black)
).withTintColor(color, renderingMode: .alwaysOriginal)

let image = UIImage.create(symbol: symbol, color: color, pointSize: size, weight: .black)

ってなるノリです

SF Symbols 使ってみて思ったこと

  • UIImage or Image でしか使えない (ですよね?) なので、FontAwesome より使い勝手が劣る部分がある
    • NSAttributedString の中で使うときとか、FontAwesome なら font を指定するノリで使えるけど、SF Symbols は NSTextAttachment を使う必要があったり
  • SF Symbols 生成される画像のサイズ感がいまいちわからない
    • 狙った幅と高さの画像を作るのが面倒
  • weight を指定できるのは良いところ
    • とはいえ default or black ぐらいしか使わなそうかもだけど
12
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?