Skip to main content
Language

From App Bundle

PhotoEditor SDK supports adding custom fonts from the app bundle. In this example, we add the font Raleway.

Creating Font objects#

We create a URL pointing to the font's location in the app bundle and pass it to the Font constructor. A Font object is a simple collection of the metadata of a font, the fontName, the displayName and the url for non system fonts. We can also create another system font using only the fontName.

Add fonts to asset catalog#

In order to add the new fonts to our asset catalog, we first add the default items and then append our fonts to the fonts array.

import PhotoEditorSDK
import UIKit
class PhotoAddFontsFromAppBundleSwift: Example, PhotoEditViewControllerDelegate {
override func invokeExample() {
// Create a `Photo` from a URL to an image in the app bundle.
let photo = Photo(url: Bundle.main.url(forResource: "LA", withExtension: "jpg")!)
// Create a reference to a custom font.
let fontURL = Bundle.main.url(forResource: "custom_font_raleway_regular", withExtension: "ttf")
let customFont = Font(url: fontURL!, displayName: "Raleway", fontName: "custom_font_raleway_regular", identifier: "custom_font_raleway_regular")
// Create a reference to a system font.
let systemFont = Font(displayName: "Helvetica", fontName: "Helvetica", identifier: "system_font_helvetica")
// Create a `Configuration` object.
let configuration = Configuration { builder in
// In this example we are using the default assets for the asset catalog as a base.
// However, you can also create an empty catalog as well which only contains your
// custom assets.
let assetCatalog = AssetCatalog.defaultItems
// Add the custom fonts to the asset catalog.
assetCatalog.fonts.append(contentsOf: [customFont, systemFont])
// Use the new asset catalog for the configuration.
builder.assetCatalog = assetCatalog
}
// Create and present the photo editor. Make this class the delegate of it to handle export and cancelation.
let photoEditViewController = PhotoEditViewController(photoAsset: photo, configuration: configuration)
photoEditViewController.delegate = self
photoEditViewController.modalPresentationStyle = .fullScreen
presentingViewController?.present(photoEditViewController, animated: true, completion: nil)
}
// MARK: - PhotoEditViewControllerDelegate
func photoEditViewControllerShouldStart(_ photoEditViewController: PhotoEditViewController, task: PhotoEditorTask) -> Bool {
// Implementing this method is optional. You can perform additional validation and interrupt the process by returning `false`.
true
}
func photoEditViewControllerDidFinish(_ photoEditViewController: PhotoEditViewController, result: PhotoEditorResult) {
// The image has been exported successfully and is passed as an `Data` object in the `result.output.data`.
// To create an `UIImage` from the output, use `UIImage(data:)`.
// See other examples about how to save the resulting image.
presentingViewController?.dismiss(animated: true, completion: nil)
}
func photoEditViewControllerDidFail(_ photoEditViewController: PhotoEditViewController, error: PhotoEditorError) {
// There was an error generating the photo.
print(error.localizedDescription)
// Dismissing the editor.
presentingViewController?.dismiss(animated: true, completion: nil)
}
func photoEditViewControllerDidCancel(_ photoEditViewController: PhotoEditViewController) {
// The user tapped on the cancel button within the editor. Dismissing the editor.
presentingViewController?.dismiss(animated: true, completion: nil)
}
}