Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore error if font was already registered #68

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions Source/SwiftIcons.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -730,13 +734,15 @@ private class FontLoader {
let font = CGFont(provider!)!

var error: Unmanaged<CFError>?
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
}
}
}
Expand Down