Customizing Content Card Templates
This tutorial explains how to customize the UI of content card in your application.
Overview
Messaging extension provides a way to customize content cards based on the template type. You can customize the content card templates using the ContentCardCustomizing protocol.
Implementing ContentCardCustomizing
Perform the following steps to customize content card templates:
- Conform to the ContentCardCustomizing protocol in your class or struct.
- Implement the desired methods of the ContentCardCustomizing protocol.
Below is an example implementation of ContentCardCustomizing
. In this example, the HomePageCardCustomizer
class conforms to the ContentCardCustomizing
protocol and customizes the SmallImageTemplate
content card template:
Copied to your clipboardclass HomePageCardCustomizer: ContentCardCustomizing {func customize(template: SmallImageTemplate) {// customize UI elementstemplate.title.textColor = .primarytemplate.title.font = .subheadlinetemplate.body?.textColor = .secondarytemplate.buttons?.first?.text.font = .system(size: 13)// customize stack structuretemplate.rootHStack.spacing = 15template.textVStack.alignment = .leadingtemplate.textVStack.spacing = 10// add custom modifierstemplate.buttonHStack.modifier = AEPViewModifier(ButtonHStackModifier())template.rootHStack.modifier = AEPViewModifier(RootHStackModifier())// customize the dismiss buttonstemplate.dismissButton?.image.iconColor = .primarytemplate.dismissButton?.image.iconFont = .system(size: 10)}struct RootHStackModifier : ViewModifier {func body(content: Content) -> some View {content.frame(maxWidth: .infinity, alignment: .leading).padding(.trailing)}}struct ButtonHStackModifier : ViewModifier {func body(content: Content) -> some View {content.frame(maxWidth: .infinity, alignment: .trailing)}}}
Applying Customizations
To apply the customizations to the content card templates, pass the customizer to the getContentCardsUI
API. The customizer will be called for each content card template type that is recognized by the Messaging extension.
Copied to your clipboardlet homePageSurface = Surface(path: "homepage")let homePageCardCustomizer = HomePageCardCustomizer()Messaging.getContentCardsUI(for: homePageSurface,customizer: homePageCardCustomizer) { result in// handle result}
Customize the content card templates for different surfaces by creating a customizer for each surface. For example, the following code snippet customizes the content card templates for the "homepage" and "detailpage" content cards separately:
Copied to your clipboardlet homePageSurface = Surface(path: "homepage")let homePageCustomizer = HomePageCardCustomizer()Messaging.getContentCardsUI(for: homePageSurface,customizer: homePageCustomizer) { result in// handle result}let detailPageSurface = Surface(path: "detailpage")let detailPageCustomizer = DetailPageCardCustomizer()Messaging.getContentCardsUI(for: detailPageSurface,customizer: detailPageCustomizer) { result in// handle result}