Skip to content

Instantly share code, notes, and snippets.

@gk-plastics
gk-plastics / SFSymbolWithFont.swift
Last active April 24, 2020 06:08
UIImage extension that returns a SFSymbol image that matches to the specified font
extension UIImage {
static func symbol(named systemName: String, font: UIFont? = nil) -> UIImage? {
if #available(iOS 13.0, *) {
let image: UIImage?
if let font = font {
let symbolConfiguration = UIImage.SymbolConfiguration(font: font)
image = UIImage(systemName: systemName, withConfiguration: symbolConfiguration)
} else {
image = UIImage(systemName: systemName)
}
@gk-plastics
gk-plastics / SetTextPrependedBySFSymbols.swift
Created April 22, 2020 15:30
UILabel extension that sets text prepended by a SF Symbols image
extension UILabel {
func setText(_ text: String, prependedBySymbolNameed symbolSystemName: String, font: UIFont? = nil) {
if #available(iOS 13.0, *) {
if let font = font { self.font = font }
let symbolConfiguration = UIImage.SymbolConfiguration(font: self.font)
let symbolImage = UIImage(systemName: symbolSystemName, withConfiguration: symbolConfiguration)?.withRenderingMode(.alwaysTemplate)
let symbolTextAttachment = NSTextAttachment()
symbolTextAttachment.image = symbolImage
let attributedText = NSMutableAttributedString()
attributedText.append(NSAttributedString(attachment: symbolTextAttachment))