Skip to content

Commit

Permalink
Merge pull request #103 from salemove/yuriid/MOB-902/0
Browse files Browse the repository at this point in the history
Remove using of fatalError to avoid crash when font can't be registered
  • Loading branch information
dukhovnyi authored Sep 6, 2021
2 parents 13730b1 + 6e0e677 commit 38454e3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
21 changes: 13 additions & 8 deletions GliaWidgets/Resources/FontProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,35 @@ internal class FontProvider {
}

func font(named: String, size: CGFloat) -> UIFont {
// swiftlint:disable force_unwrapping
return UIFont(name: named, size: size)!
return UIFont(name: named, size: size) ?? .systemFont(ofSize: size)
}

private func loadFonts() {
fonts.forEach { loadFont(named: $0) }
_ = fonts.first(where: { !loadFont(named: $0) })
}

private func loadFont(named name: String) {
private func loadFont(named name: String) -> Bool {
let bundle = Bundle(for: BundleToken.self)

guard let pathForResourceString = bundle.path(forResource: name, ofType: "ttf"),
guard
let pathForResourceString = bundle.path(forResource: name, ofType: "ttf"),
let fontData = NSData(contentsOfFile: pathForResourceString),
let dataProvider = CGDataProvider(data: fontData),
let fontRef = CGFont(dataProvider) else {
fatalError("Could not load fonts. Perhaps they are not inculded in the bundle?")
let fontRef = CGFont(dataProvider)
else {
print("Could not load font='\(name)'. Perhaps they are not inculded in the bundle?")
return false
}

var errorRef: Unmanaged<CFError>?
let registrationResult = CTFontManagerRegisterGraphicsFont(fontRef, &errorRef)

if !registrationResult {
fatalError("Could not load fonts")
print("Font='\(name)' has not been registered properly. Error='\(errorRef.debugDescription)'.")
return false
}

return true
}
}

Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ pod install
xed GliaWidgets.xcworkspace
```

## Cocoapods

For integration, the `GliaWidgets` into your project via [CocoaPods](https://cocoapods.org/) modify your `Podfile`
```
# Podfile
source 'https://github.com/CocoaPods/Specs.git'
source 'https://github.com/salemove/glia-ios-podspecs.git'
user_frameworks!
target 'YOUR_TARGET_NAME' do
pod 'GliaWidgets', git: 'https://github.com/salemove/ios-sdk-widgets', tag: 'SDK_VERSION'
end
```

## Communication

Expand Down

0 comments on commit 38454e3

Please sign in to comment.