需求發(fā)現(xiàn)
我們應(yīng)用要在自定義的 WKWebView 支持 Safari 打開(kāi)可以跳轉(zhuǎn)的 url,所以需要添加一個(gè)navigationItem 的 Safari 圖標(biāo)可以用來(lái)操作,這時(shí)記起來(lái)蘋果自帶很多圖標(biāo),比如 add菇晃,search 等一系列,所以想找到 Safari 的圖標(biāo)崎场,結(jié)果進(jìn)一步探究引出下文埋泵。
簡(jiǎn)介
SF Symbols provides a set of over 1,500 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 for all weights and sizes. You can use SF Symbols in apps running in iOS 13 and later, watchOS 6 and later, and tvOS 13 and later.
總結(jié)為: SF Symbols 是蘋果在2019年推出自 iOS 13 開(kāi)始支持的一套1500+的高度可配置的符號(hào)
,旨在規(guī)范和簡(jiǎn)化圖標(biāo)的制作和集成么介。
下載地址: https://developer.apple.com/design/downloads/SF-Symbols.dmg
當(dāng)然娜遵,如果符號(hào)不符合你的要求,你也可以導(dǎo)出 svg 格式的模板重新編輯壤短,然后再導(dǎo)入到XCode中设拟,使用 UIImage(name: "safari")調(diào)用,默認(rèn)會(huì)優(yōu)先調(diào)用你自定義的 symbols 符號(hào) (所以也要避免自定義的符號(hào)和 @2x 普通圖標(biāo)重名)久脯。
使用
大家可以看到纳胧,符號(hào)的使用是 UIImage 的一個(gè) init 方法
使用起來(lái)也很方便,只需要:
if #available(iOS 13.0, *) {
let image = UIImage(systemName: "safari")
...
}
上面說(shuō)到這是一款靈活可配置的符號(hào)
帘撰,那么靈活在哪里呢跑慕?那么來(lái)看看下面方法:
@available(iOS 13.0, *)
public /*not inherited*/ init?(systemName name: String, withConfiguration configuration: UIImage.Configuration?)
可以通過(guò)設(shè)置 configuration 設(shè)置一些屬性,更多見(jiàn) UIImage.SymbolConfiguration
let config = UIImage.SymbolConfiguration(pointSize: 30, weight: UIImage.SymbolWeight.medium)
let image = UIImage(systemName: "safari", withConfiguration: config)
...
因?yàn)?Configuration 是 UIImage 的屬性,所以也可以直接對(duì)
ImageView 設(shè)置 preferredSymbolConfiguration
屬性:
let image = UIImage(systemName: "safari")
let imageView = UIImageView(image: image)
let config = UIImage.SymbolConfiguration(pointSize: 30, weight: UIImage.SymbolWeight.medium)
imageView.preferredSymbolConfiguration = config
...
更多詳細(xì)使用見(jiàn) https://developer.apple.com/documentation/uikit/uiimage/3294233-init
總結(jié)
這是蘋果一次走向規(guī)范的不錯(cuò)嘗試核行,之后 API 還會(huì)支持以NSTextAttachment 形式嵌入到屬性字符串文本中牢硅。
但是使用的同時(shí)也存在一些問(wèn)題,比如 iOS13 以下得另行適配芝雪,符號(hào)庫(kù)遠(yuǎn)遠(yuǎn)不夠豐富减余,一些符合(比如 safari)不支持導(dǎo)出自定義等等。
總之惩系,期待更多的開(kāi)放和支持 Do it and do better位岔。
謝謝閱讀(?▽?)
參考
- SF Symbols 蘋果官網(wǎng)文檔 https://developer.apple.com/design/human-interface-guidelines/sf-symbols/overview/
- Introducing SF Symbols - WWDC 2019 https://developer.apple.com/videos/play/wwdc2019/206/
- 解讀 WWDC19 - SF Symbols 內(nèi)置圖標(biāo)庫(kù) https://swiftcafe.io/post/sf-symbol