原生範本

選取平台: Android iOS Flutter

下載原生範本

使用原生廣告可以自訂廣告內容,提升使用者體驗,進而提高參與度和整體收益。

為了讓原生廣告發揮最大效果,建議將廣告版面配置設計得像應用程式的一部分,自然融入整體介面。我們也提供原生範本來協助您快速上手。

原生範本是附帶完整程式碼的檢視區塊,方便快速套用來顯示原生廣告,且易於修改。有了原生範本,只要幾分鐘就能導入第一則原生廣告,並可迅速自訂外觀和風格,無需撰寫大量程式碼。這些範本可以放在任何地方,例如新聞動態消息中的 TableView、對話方塊,或應用程式的任何其他位置。

本指南說明如何下載、整合並在 iOS 應用程式中使用原生範本。開始前,請確認您已成功使用 SDK 載入原生廣告。

範本尺寸

範本分成小型和中型兩種尺寸。每種範本都對應一個類別,分別是 GADTSmallTemplateViewGADTMediumTemplateView,兩者皆繼承自 GADTTemplateView。兩種範本都有固定的顯示比例,只有在您呼叫 addHorizontalConstraintsToSuperviewWidth 後,才會依上層檢視區塊的寬度縮放;如未呼叫 addHorizontalConstraintsToSuperviewWidth,則會以預設大小呈現。

GADTSmallTemplateView

小型範本適合放在 UICollectionViewUITableView 儲存格中,例如用於動態內廣告,或其他需要細長矩形廣告檢視區塊的地方。預設大小為高 91 點,寬 355 點。

GADTMediumTemplateView

中型範本約占半頁到四分之三頁,適合用於到達網頁或啟動頁面,也可以嵌入 UITableViews 使用。預設大小為高 370 點,寬 355 點。

所有範本都支援自動版面配置,擺放位置可自由調整。當然,您也可以視需要修改原始程式碼或 xib 檔案。

安裝原生廣告範本

如要安裝原生範本,只要下載 ZIP 檔案並拖曳至 Xcode 專案即可。請記得勾選「Copy items if needed」

使用原生廣告範本

將資料夾加入專案並在檔案中附上相關類別後,即可按照以下步驟使用範本。請注意,目前只能透過樣式字典修改字型和樣式屬性,而且會覆寫 xib 檔案中設定的樣式。

Objective-C

/// Step 1: Import the templates that you need.
#import "NativeTemplates/GADTSmallTemplateView.h"
#import "NativeTemplates/GADTTemplateView.h"
...

// STEP 2: Initialize your template view object.
GADTSmallTemplateView *templateView =
    [[NSBundle mainBundle] loadNibNamed:@"GADTSmallTemplateView" owner:nil options:nil]
      .firstObject;

// STEP 3: Template views are just GADNativeAdViews.
_nativeAdView = templateView;
nativeAd.delegate = self;

// STEP 4: Add your template as a subview of whichever view you'd like.
// This must be done before calling addHorizontalConstraintsToSuperviewWidth.
// Please note: Our template objects are subclasses of GADNativeAdView so
// you can insert them into whatever type of view youd like, and dont need to
// create your own.
[self.view addSubview:templateView];

// STEP 5 (Optional): Create your styles dictionary. Set your styles dictionary
// on the template property. A default dictionary is created for you if you do
// not set this. Note - templates do not currently respect style changes in the
// xib.

NSString *myBlueColor = @"#5C84F0";
NSDictionary *styles = @{
    GADTNativeTemplateStyleKeyCallToActionFont : [UIFont systemFontOfSize:15.0],
    GADTNativeTemplateStyleKeyCallToActionFontColor : UIColor.whiteColor,
    GADTNativeTemplateStyleKeyCallToActionBackgroundColor :
        [GADTTemplateView colorFromHexString:myBlueColor],
    GADTNativeTemplateStyleKeySecondaryFont : [UIFont systemFontOfSize:15.0],
    GADTNativeTemplateStyleKeySecondaryFontColor : UIColor.grayColor,
    GADTNativeTemplateStyleKeySecondaryBackgroundColor : UIColor.whiteColor,
    GADTNativeTemplateStyleKeyPrimaryFont : [UIFont systemFontOfSize:15.0],
    GADTNativeTemplateStyleKeyPrimaryFontColor : UIColor.blackColor,
    GADTNativeTemplateStyleKeyPrimaryBackgroundColor : UIColor.whiteColor,
    GADTNativeTemplateStyleKeyTertiaryFont : [UIFont systemFontOfSize:15.0],
    GADTNativeTemplateStyleKeyTertiaryFontColor : UIColor.grayColor,
    GADTNativeTemplateStyleKeyTertiaryBackgroundColor : UIColor.whiteColor,
    GADTNativeTemplateStyleKeyMainBackgroundColor : UIColor.whiteColor,
    GADTNativeTemplateStyleKeyCornerRadius : [NSNumber numberWithFloat:7.0],
};

templateView.styles = styles;

// STEP 6: Set the ad for your template to render.
templateView.nativeAd = nativeAd;

// STEP 7 (Optional): If you'd like your template view to span the width of your
// superview call this method.
[templateView addHorizontalConstraintsToSuperviewWidth];
[templateView addVerticalCenterConstraintToSuperview];

樣式字典鍵

自訂範本最快的方法,就是建立包含下列鍵的字典:

Objective-C

/// Call to action font. Expects a UIFont.
GADTNativeTemplateStyleKeyCallToActionFont

/// Call to action font color. Expects a UIColor.
GADTNativeTemplateStyleKeyCallToActionFontColor;

/// Call to action background color. Expects a UIColor.
GADTNativeTemplateStyleKeyCallToActionBackgroundColor;

/// The font, font color and background color for the first row of text in the
/// template.

/// All templates have a primary text area which is populated by the native ad's
/// headline.

/// Primary text font. Expects a UIFont.
GADTNativeTemplateStyleKeyPrimaryFont;

/// Primary text font color. Expects a UIFont.
GADTNativeTemplateStyleKeyPrimaryFontColor;

/// Primary text background color. Expects a UIColor.
GADTNativeTemplateStyleKeyPrimaryBackgroundColor;

/// The font, font color and background color for the second row of text in the
/// template.

/// All templates have a secondary text area which is populated either by the
/// body of the ad, or by the rating of the app.

/// Secondary text font. Expects a UIFont.
GADTNativeTemplateStyleKeySecondaryFont;

/// Secondary text font color. Expects a UIColor.
GADTNativeTemplateStyleKeySecondaryFontColor;

/// Secondary text background color. Expects a UIColor.
GADTNativeTemplateStyleKeySecondaryBackgroundColor;

/// The font, font color and background color for the third row of text in the
/// template. The third row is used to display store name or the default
/// tertiary text.

/// Tertiary text font. Expects a UIFont.
GADTNativeTemplateStyleKeyTertiaryFont;

/// Tertiary text font color. Expects a UIColor.
GADTNativeTemplateStyleKeyTertiaryFontColor;

/// Tertiary text background color. Expects a UIColor.
GADTNativeTemplateStyleKeyTertiaryBackgroundColor;

/// The background color for the bulk of the ad. Expects a UIColor.
GADTNativeTemplateStyleKeyMainBackgroundColor;

/// The corner rounding radius for the icon view and call to action. Expects an
/// NSNumber.
GADTNativeTemplateStyleKeyCornerRadius;

常見問題

為什麼在例項化範本物件時會出現例外狀況?
如果您在 xib 檔案中修改了檢視區塊的大小,但未同步修改子類別「setup」方法中建立的框架大小,就可能發生這種情況。
如何進一步自訂這些範本?
這些範本其實就是附帶對應檢視區塊物件的 xib,使用方式與 iOS 開發中常見的其他 xib 和自訂檢視區塊類別相同。如果想從零開始打造原生廣告,請參閱原生進階指南
為什麼在 xib 中設定的樣式沒有生效?
目前 GADTTemplateView.m 的預設樣式字典會覆寫 xib 中設定的所有樣式。

貢獻己力

為協助快速開發廣告,我們製作了許多原生範本,也歡迎您到 GitHub 存放區貢獻新的範本或功能。只要傳送提取要求,我們就會著手處理。