From 1709bdab3708dd9b7ea3860abbfdb12bd7dbcdf3 Mon Sep 17 00:00:00 2001 From: Nam Kennic <61685543+vnamnh70@users.noreply.github.com> Date: Mon, 2 Aug 2021 22:08:08 +0700 Subject: [PATCH 1/2] Ignore error if font was already registered --- Source/SwiftIcons.swift | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/Source/SwiftIcons.swift b/Source/SwiftIcons.swift index e8f3442..b665546 100644 --- a/Source/SwiftIcons.swift +++ b/Source/SwiftIcons.swift @@ -730,13 +730,15 @@ private class FontLoader { let font = CGFont(provider!)! var error: Unmanaged? - if !CTFontManagerRegisterGraphicsFont(font, &error) { - let errorDescription: CFString = CFErrorCopyDescription(error!.takeUnretainedValue()) - let nsError = error!.takeUnretainedValue() as AnyObject as! NSError - NSException(name: NSExceptionName.internalInconsistencyException, reason: errorDescription as String, userInfo: [NSUnderlyingErrorKey: nsError]).raise() - } else { - loadedFontsTracker[fontName] = true - } + if !CTFontManagerRegisterGraphicsFont(font, &error) { + let errorDescription: CFString = CFErrorCopyDescription(error!.takeUnretainedValue()) + let nsError = error!.takeUnretainedValue() as AnyObject as! NSError + if nsError.code != 105 { + NSException(name: NSExceptionName.internalInconsistencyException, reason: errorDescription as String, userInfo: [NSUnderlyingErrorKey: nsError]).raise() + } + } + + loadedFontsTracker[fontName] = true } } } From 3bbe3b2497124e3148e27c6e4754e55c4e1da391 Mon Sep 17 00:00:00 2001 From: Nam Kennic Date: Sun, 5 Nov 2023 17:53:09 +0700 Subject: [PATCH 2/2] Don't crash --- Source/SwiftIcons.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Source/SwiftIcons.swift b/Source/SwiftIcons.swift index b665546..22f0381 100644 --- a/Source/SwiftIcons.swift +++ b/Source/SwiftIcons.swift @@ -43,7 +43,11 @@ public extension UIImage { let fontAspectRatio: CGFloat = 1.28571429 let fontSize = min(size.width / fontAspectRatio, size.height) let font = UIFont(name: icon.fontName(), size: fontSize) - assert(font != nil, icon.errorAnnounce()) + if font == nil { + self.init() + return + } // prevent unnecessary crash + let attributes = [NSAttributedString.Key.font: font!, NSAttributedString.Key.foregroundColor: textColor, NSAttributedString.Key.backgroundColor: backgroundColor, NSAttributedString.Key.paragraphStyle: paragraph] let lineHeight = font!.lineHeight let attributedString = NSAttributedString(string: icon.text!, attributes: attributes)