Skip to main content
PESDK/Android/Guides/Text & Fonts/Add Fonts
Language:

From Remote URL

PhotoEditor SDK supports loading fonts from a remote URL, this can be a resource hosted by a file hosting provider or your servers. Although the editor supports adding assets with remote URLs, we highly recommend that you manage the download of remote resources yourself, since this gives you more control over the whole process.

Download font#

Here, we create a File in the cache directory and download the remote font. Since file handling and downloading are IO operations, we do this inside a coroutine using the IO dispatcher.

Create FontAsset#

Here, we create a FontAsset with a unique identifier and the font file downloaded above.

Add font to AssetConfig#

To use the font, it must first be available in the SDK's backend. This is done by adding the FontAsset to the AssetConfig.

Configure UiConfigText#

Here, we configure UiConfigText and add a custom FontItem to the fontList.

File:
class PhotoAddFontsFromRemoteURL(private val activity: AppCompatActivity) : Example(activity) {
// Although the editor supports adding assets with remote URLs, we highly recommend
// that you manage the download of remote resources yourself, since this
// gives you more control over the whole process.
override fun invoke() {
activity.lifecycleScope.launch {
showLoader(true)
val file = withContext(Dispatchers.IO) {
runCatching {
// Create file in cache directory
val file = File(activity.cacheDir, "raleway.ttf")
if (file.exists()) file.delete()
file.createNewFile()
// Save remote resource to file
URL("https://img.ly/static/example-assets/custom_font_raleway_regular.ttf").openStream().use { input ->
file.outputStream().use { output ->
input.copyTo(output)
}
}
file
}.getOrNull()
}
showLoader(false)
file?.let {
showEditor(it)
} ?: showMessage("Error downloading the file")
}
}
private fun showEditor(fontFile: File) {
// In this example, we do not need access to the Uri(s) after the editor is closed
// so we pass false in the constructor
val settingsList = PhotoEditorSettingsList(false)
// Set the source as the Uri of the image to be loaded
.configure<LoadSettings> {
it.source = activity.resourceUri(R.drawable.la)
}
// Create a custom font
val customFont = FontAsset("custom_font_raleway_regular", fontFile)
// Add asset to AssetConfig
settingsList.config.addAsset(customFont)
settingsList.configure<UiConfigText> {
// Add custom font to the UI
it.fontList.add(FontItem("custom_font_raleway_regular", "Raleway"))
}
// Start the photo editor using PhotoEditorBuilder
// The result will be obtained in onActivityResult() corresponding to EDITOR_REQUEST_CODE
PhotoEditorBuilder(activity)
.setSettingsList(settingsList)
.startActivityForResult(activity, EDITOR_REQUEST_CODE)
// Release the SettingsList once done
settingsList.release()
}
override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
intent ?: return
if (requestCode == EDITOR_REQUEST_CODE) {
// Wrap the intent into an EditorSDKResult
val result = EditorSDKResult(intent)
when (result.resultStatus) {
EditorSDKResult.Status.CANCELED -> showMessage("Editor cancelled")
EditorSDKResult.Status.EXPORT_DONE -> showMessage("Result saved at ${result.resultUri}")
else -> {
}
}
}
}
}