b面1.0.6广告提交版

This commit is contained in:
Mr.zhou 2024-06-19 18:32:56 +08:00
parent d85510f2fc
commit c1d40ee346
5 changed files with 599 additions and 0 deletions

View File

@ -0,0 +1,22 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import "GADTTemplateView.h"
/// A small template, perfect for UITableViewCells or other row views.
@interface GADTSmallTemplateView : GADTTemplateView
@end

View File

@ -0,0 +1,30 @@
// Copyright 2018-2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import "GADTSmallTemplateView.h"
@implementation GADTSmallTemplateView
- (nonnull instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.translatesAutoresizingMaskIntoConstraints = NO;
}
return self;
}
- (nonnull NSString *)getTemplateTypeName {
return @"small_template";
}
@end

View File

@ -0,0 +1,146 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="32700.99.1234" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina5_5" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="22684"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="GADTSmallTemplateView"/>
<view opaque="NO" clipsSubviews="YES" clearsContextBeforeDrawing="NO" contentMode="scaleToFill" insetsLayoutMarginsFromSafeArea="NO" translatesAutoresizingMaskIntoConstraints="NO" id="h2X-7h-r2O" customClass="GADTTemplateView">
<rect key="frame" x="0.0" y="0.0" width="375" height="150"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="L39-RW-Fxr">
<rect key="frame" x="0.0" y="26.333333333333329" width="120" height="119.99999999999999"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="6ux-FJ-pGo" customClass="GADMediaView">
<rect key="frame" x="0.0" y="0.0" width="120" height="120"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</view>
</subviews>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstItem="6ux-FJ-pGo" firstAttribute="top" secondItem="L39-RW-Fxr" secondAttribute="top" id="281-CJ-L7V"/>
<constraint firstAttribute="bottom" secondItem="6ux-FJ-pGo" secondAttribute="bottom" id="91P-YK-0vm"/>
<constraint firstAttribute="width" constant="120" id="fj8-1A-ich">
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="boolean" keyPath="adapterScreen" value="YES"/>
</userDefinedRuntimeAttributes>
</constraint>
<constraint firstAttribute="trailing" secondItem="6ux-FJ-pGo" secondAttribute="trailing" id="gMj-0R-mCS"/>
<constraint firstItem="6ux-FJ-pGo" firstAttribute="leading" secondItem="L39-RW-Fxr" secondAttribute="leading" id="sEZ-Ep-wvx"/>
<constraint firstAttribute="height" constant="120" id="yAa-pt-xcq">
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="boolean" keyPath="adapterScreen" value="YES"/>
</userDefinedRuntimeAttributes>
</constraint>
</constraints>
</view>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="9ag-e3-7oH">
<rect key="frame" x="130" y="105" width="235" height="40"/>
<color key="backgroundColor" red="0.50196078431372548" green="0.97647058823529409" blue="0.53333333333333333" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="height" constant="40" id="pEx-ZJ-Uiq">
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="boolean" keyPath="adapterScreen" value="YES"/>
</userDefinedRuntimeAttributes>
</constraint>
</constraints>
<state key="normal" title="Button">
<color key="titleColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</state>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="cornerRadius">
<real key="value" value="5"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</button>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="pWT-IG-tgy">
<rect key="frame" x="130" y="78.666666666666671" width="235" height="14.333333333333329"/>
<fontDescription key="fontDescription" type="system" pointSize="12"/>
<color key="textColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Ad" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="hl4-7y-gXg">
<rect key="frame" x="0.0" y="0.0" width="25" height="25"/>
<color key="backgroundColor" red="0.94057513299999995" green="0.67334330669999998" blue="0.30227726059999999" alpha="1" colorSpace="calibratedRGB"/>
<constraints>
<constraint firstAttribute="width" constant="25" id="QH3-cb-lPf"/>
<constraint firstAttribute="height" constant="25" id="SOp-Hf-Z4i"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="12"/>
<color key="textColor" red="0.2274509804" green="0.4039215686" blue="0.15686274510000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="cornerRadius">
<real key="value" value="3"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Zjv-ch-qcM">
<rect key="frame" x="130" y="54.333333333333336" width="235" height="14.333333333333336"/>
<fontDescription key="fontDescription" type="system" pointSize="12"/>
<color key="textColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Headline" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="DKj-id-Sm4">
<rect key="frame" x="130" y="26.333333333333329" width="235" height="18"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="15"/>
<color key="textColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<accessibility key="accessibilityConfiguration">
<accessibilityTraits key="traits" notEnabled="YES"/>
<bool key="isElement" value="YES"/>
</accessibility>
<constraints>
<constraint firstItem="DKj-id-Sm4" firstAttribute="leading" secondItem="L39-RW-Fxr" secondAttribute="trailing" constant="10" id="6gc-Wt-zRU">
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="boolean" keyPath="adapterScreen" value="YES"/>
</userDefinedRuntimeAttributes>
</constraint>
<constraint firstAttribute="bottom" secondItem="9ag-e3-7oH" secondAttribute="bottom" constant="5" id="79h-8e-zUt"/>
<constraint firstItem="L39-RW-Fxr" firstAttribute="centerY" secondItem="h2X-7h-r2O" secondAttribute="centerY" multiplier="1.15" id="8eU-K6-1JI"/>
<constraint firstItem="Zjv-ch-qcM" firstAttribute="top" secondItem="DKj-id-Sm4" secondAttribute="bottom" constant="10" id="BQa-eh-n2q">
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="boolean" keyPath="adapterScreen" value="YES"/>
</userDefinedRuntimeAttributes>
</constraint>
<constraint firstItem="9ag-e3-7oH" firstAttribute="leading" secondItem="DKj-id-Sm4" secondAttribute="leading" id="C3T-wU-emY"/>
<constraint firstItem="Zjv-ch-qcM" firstAttribute="trailing" secondItem="DKj-id-Sm4" secondAttribute="trailing" id="DEj-M3-UtV"/>
<constraint firstItem="9ag-e3-7oH" firstAttribute="trailing" secondItem="DKj-id-Sm4" secondAttribute="trailing" id="Evp-1O-78r"/>
<constraint firstItem="pWT-IG-tgy" firstAttribute="top" secondItem="Zjv-ch-qcM" secondAttribute="bottom" constant="10" id="TAx-uP-fwr">
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="boolean" keyPath="adapterScreen" value="YES"/>
</userDefinedRuntimeAttributes>
</constraint>
<constraint firstItem="hl4-7y-gXg" firstAttribute="top" secondItem="h2X-7h-r2O" secondAttribute="top" id="VMX-Em-mID"/>
<constraint firstItem="L39-RW-Fxr" firstAttribute="leading" secondItem="h2X-7h-r2O" secondAttribute="leading" id="Yg8-QF-edL"/>
<constraint firstItem="pWT-IG-tgy" firstAttribute="trailing" secondItem="Zjv-ch-qcM" secondAttribute="trailing" id="bR4-c1-iz1"/>
<constraint firstItem="Zjv-ch-qcM" firstAttribute="leading" secondItem="DKj-id-Sm4" secondAttribute="leading" id="gZv-4Z-LaI"/>
<constraint firstItem="hl4-7y-gXg" firstAttribute="leading" secondItem="h2X-7h-r2O" secondAttribute="leading" id="ieU-7H-gpP"/>
<constraint firstAttribute="trailing" secondItem="DKj-id-Sm4" secondAttribute="trailing" constant="10" id="mgb-7X-ol8">
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="boolean" keyPath="adapterScreen" value="YES"/>
</userDefinedRuntimeAttributes>
</constraint>
<constraint firstItem="pWT-IG-tgy" firstAttribute="leading" secondItem="Zjv-ch-qcM" secondAttribute="leading" id="mkc-Sf-V14"/>
<constraint firstItem="DKj-id-Sm4" firstAttribute="top" secondItem="L39-RW-Fxr" secondAttribute="top" id="p7H-7N-1WY"/>
</constraints>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
<connections>
<outlet property="adBadge" destination="hl4-7y-gXg" id="Bp7-FM-VVW"/>
<outlet property="advertiserView" destination="Zjv-ch-qcM" id="Ku9-M6-SEf"/>
<outlet property="callToActionView" destination="9ag-e3-7oH" id="Uh9-xd-ccJ"/>
<outlet property="headlineView" destination="DKj-id-Sm4" id="fwg-CO-DrR"/>
<outlet property="mediaView" destination="6ux-FJ-pGo" id="aCz-rd-9bD"/>
<outlet property="storeView" destination="pWT-IG-tgy" id="f3q-8l-We7"/>
</connections>
<point key="canvasLocation" x="-67.391304347826093" y="664.4021739130435"/>
</view>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
</objects>
</document>

View File

@ -0,0 +1,100 @@
// Copyright 2018-2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import <GoogleMobileAds/GoogleMobileAds.h>
/// Constants used to style your template.
typedef NSString* GADTNativeTemplateStyleKey NS_STRING_ENUM;
/// The font, font color and background color for your call to action view.
/// All templates have a call to action view.
#pragma mark - Call To Action
/// Call to action font. Expects a UIFont.
extern GADTNativeTemplateStyleKey const GADTNativeTemplateStyleKeyCallToActionFont;
/// Call to action font color. Expects a UIColor.
extern GADTNativeTemplateStyleKey const GADTNativeTemplateStyleKeyCallToActionFontColor;
/// Call to action background color. Expects a UIColor.
extern GADTNativeTemplateStyleKey const 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.
#pragma mark - Primary Text
/// Primary text font. Expects a UIFont.
extern GADTNativeTemplateStyleKey const GADTNativeTemplateStyleKeyPrimaryFont;
/// Primary text font color. Expects a UIColor.
extern GADTNativeTemplateStyleKey const GADTNativeTemplateStyleKeyPrimaryFontColor;
/// Primary text background color. Expects a UIColor.
extern GADTNativeTemplateStyleKey const 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.
#pragma mark - Secondary Text
/// Secondary text font. Expects a UIFont.
extern GADTNativeTemplateStyleKey const GADTNativeTemplateStyleKeySecondaryFont;
/// Secondary text font color. Expects a UIColor.
extern GADTNativeTemplateStyleKey const GADTNativeTemplateStyleKeySecondaryFontColor;
/// Secondary text background color. Expects a UIColor.
extern GADTNativeTemplateStyleKey const 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.
#pragma mark - Tertiary Text
/// Tertiary text font. Expects a UIFont.
extern GADTNativeTemplateStyleKey const GADTNativeTemplateStyleKeyTertiaryFont;
/// Tertiary text font color. Expects a UIColor.
extern GADTNativeTemplateStyleKey const GADTNativeTemplateStyleKeyTertiaryFontColor;
/// Tertiary text background color. Expects a UIColor.
extern GADTNativeTemplateStyleKey const GADTNativeTemplateStyleKeyTertiaryBackgroundColor;
#pragma mark - Additional Style Options
/// The background color for the bulk of the ad. Expects a UIColor.
extern GADTNativeTemplateStyleKey const GADTNativeTemplateStyleKeyMainBackgroundColor;
/// The corner rounding radius for the icon view and call to action. Expects an NSNumber.
extern GADTNativeTemplateStyleKey const GADTNativeTemplateStyleKeyCornerRadius;
/// The super class for every template object.
/// This class has the majority of all layout and styling logic.
@interface GADTTemplateView : GADNativeAdView
@property(nonatomic, copy) NSDictionary<GADTNativeTemplateStyleKey, NSObject *> *styles;
@property(weak) IBOutlet UILabel *adBadge;
@property(weak) UIView *rootView;
/// Adds a constraint to the superview so that the template spans the width of its parent.
/// Does nothing if there is no superview.
- (void)addHorizontalConstraintsToSuperviewWidth;
/// Adds a constraint to the superview so that the template is centered vertically in its parent.
/// Does nothing if there is no superview.
- (void)addVerticalCenterConstraintToSuperview;
/// Utility method to get a color from a hex string.
+ (UIColor *)colorFromHexString:(NSString *)hexString;
- (NSString *)getTemplateTypeName;
@end

View File

@ -0,0 +1,301 @@
// Copyright 2018-2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import "GADTTemplateView.h"
#import <QuartzCore/QuartzCore.h>
GADTNativeTemplateStyleKey const GADTNativeTemplateStyleKeyCallToActionFont =
@"call_to_action_font";
GADTNativeTemplateStyleKey const GADTNativeTemplateStyleKeyCallToActionFontColor =
@"call_to_action_font_color";
GADTNativeTemplateStyleKey const GADTNativeTemplateStyleKeyCallToActionBackgroundColor =
@"call_to_action_background_color";
GADTNativeTemplateStyleKey const GADTNativeTemplateStyleKeySecondaryFont = @"secondary_font";
GADTNativeTemplateStyleKey const GADTNativeTemplateStyleKeySecondaryFontColor =
@"secondary_font_color";
GADTNativeTemplateStyleKey const GADTNativeTemplateStyleKeySecondaryBackgroundColor =
@"secondary_background_color";
GADTNativeTemplateStyleKey const GADTNativeTemplateStyleKeyPrimaryFont = @"primary_font";
GADTNativeTemplateStyleKey const GADTNativeTemplateStyleKeyPrimaryFontColor = @"primary_font_color";
GADTNativeTemplateStyleKey const GADTNativeTemplateStyleKeyPrimaryBackgroundColor =
@"primary_background_color";
GADTNativeTemplateStyleKey const GADTNativeTemplateStyleKeyTertiaryFont = @"tertiary_font";
GADTNativeTemplateStyleKey const GADTNativeTemplateStyleKeyTertiaryFontColor =
@"tertiary_font_color";
GADTNativeTemplateStyleKey const GADTNativeTemplateStyleKeyTertiaryBackgroundColor =
@"tertiary_background_color";
GADTNativeTemplateStyleKey const GADTNativeTemplateStyleKeyMainBackgroundColor =
@"main_background_color";
GADTNativeTemplateStyleKey const GADTNativeTemplateStyleKeyCornerRadius = @"corner_radius";
static NSString* const GADTBlue = @"#5C84F0";
@implementation GADTTemplateView {
NSDictionary<GADTNativeTemplateStyleKey, NSObject*>* _defaultStyles;
}
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
_rootView = [NSBundle.mainBundle loadNibNamed:NSStringFromClass([self class])
owner:self
options:nil]
.firstObject;
[self addSubview:_rootView];
[self
addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:|[_rootView]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(_rootView)]];
[self
addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:|[_rootView]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(_rootView)]];
[self applyStyles];
}
return self;
}
- (NSString *)getTemplateTypeName {
return @"root";
}
/// Returns the style value for the provided key or the default style if no styles dictionary
/// was set.
- (id)styleForKey:(GADTNativeTemplateStyleKey)key {
return _styles[key] ?: nil;
}
// Goes through all recognized style keys and updates the views accordingly, overwriting the
// defaults.
- (void)applyStyles {
self.layer.borderColor = [GADTTemplateView colorFromHexString:@"E0E0E0"].CGColor;
self.layer.borderWidth = 1.0f;
[self.mediaView sizeToFit];
if ([self styleForKey:GADTNativeTemplateStyleKeyCornerRadius]) {
float roundedCornerRadius =
((NSNumber *)[self styleForKey:GADTNativeTemplateStyleKeyCornerRadius]).floatValue;
// Rounded corners
self.iconView.layer.cornerRadius = roundedCornerRadius;
self.iconView.clipsToBounds = YES;
((UIButton*)self.callToActionView).layer.cornerRadius = roundedCornerRadius;
((UIButton*)self.callToActionView).clipsToBounds = YES;
}
// Fonts
if ([self styleForKey:GADTNativeTemplateStyleKeyPrimaryFont]) {
((UILabel *)self.headlineView).font =
(UIFont *)[self styleForKey:GADTNativeTemplateStyleKeyPrimaryFont];
}
if ([self styleForKey:GADTNativeTemplateStyleKeySecondaryFont]) {
((UILabel *)self.bodyView).font =
(UIFont *)[self styleForKey:GADTNativeTemplateStyleKeySecondaryFont];
}
if ([self styleForKey:GADTNativeTemplateStyleKeyTertiaryFont]) {
((UILabel *)self.advertiserView).font =
(UIFont *)[self styleForKey:GADTNativeTemplateStyleKeyTertiaryFont];
}
if ([self styleForKey:GADTNativeTemplateStyleKeyCallToActionFont]) {
((UIButton *)self.callToActionView).titleLabel.font =
(UIFont *)[self styleForKey:GADTNativeTemplateStyleKeyCallToActionFont];
}
// Font colors
if ([self styleForKey:GADTNativeTemplateStyleKeyPrimaryFontColor])
((UILabel *)self.headlineView).textColor =
(UIColor *)[self styleForKey:GADTNativeTemplateStyleKeyPrimaryFontColor];
if ([self styleForKey:GADTNativeTemplateStyleKeySecondaryFontColor]) {
((UILabel *)self.bodyView).textColor =
(UIColor *)[self styleForKey:GADTNativeTemplateStyleKeySecondaryFontColor];
}
if ([self styleForKey:GADTNativeTemplateStyleKeyTertiaryFontColor]) {
((UILabel *)self.advertiserView).textColor =
(UIColor *)[self styleForKey:GADTNativeTemplateStyleKeyTertiaryFontColor];
}
if ([self styleForKey:GADTNativeTemplateStyleKeyCallToActionFontColor]) {
[((UIButton *)self.callToActionView)
setTitleColor:(UIColor *)[self styleForKey:GADTNativeTemplateStyleKeyCallToActionFontColor]
forState:UIControlStateNormal];
}
// Background colors
if ([self styleForKey:GADTNativeTemplateStyleKeyPrimaryBackgroundColor]) {
((UILabel *)self.headlineView).backgroundColor =
(UIColor *)[self styleForKey:GADTNativeTemplateStyleKeyPrimaryBackgroundColor];
}
if ([self styleForKey:GADTNativeTemplateStyleKeySecondaryBackgroundColor]) {
((UILabel *)self.bodyView).backgroundColor =
(UIColor *)[self styleForKey:GADTNativeTemplateStyleKeySecondaryBackgroundColor];
}
if ([self styleForKey:GADTNativeTemplateStyleKeyTertiaryBackgroundColor]) {
((UILabel *)self.advertiserView).backgroundColor =
(UIColor *)[self styleForKey:GADTNativeTemplateStyleKeyTertiaryBackgroundColor];
}
if ([self styleForKey:GADTNativeTemplateStyleKeyCallToActionBackgroundColor]) {
((UIButton *)self.callToActionView).backgroundColor =
(UIColor *)[self styleForKey:GADTNativeTemplateStyleKeyCallToActionBackgroundColor];
}
if ([self styleForKey:GADTNativeTemplateStyleKeyMainBackgroundColor]) {
self.backgroundColor =
(UIColor *)[self styleForKey:GADTNativeTemplateStyleKeyMainBackgroundColor];
}
[self styleAdBadge];
}
- (void)styleAdBadge {
UILabel *adBadge = self.adBadge;
adBadge.layer.borderColor = adBadge.textColor.CGColor;
adBadge.layer.borderWidth = 1.0;
adBadge.layer.cornerRadius = 3.0;
adBadge.layer.masksToBounds = YES;
}
- (void)setStyles:(NSDictionary<GADTNativeTemplateStyleKey, NSObject *> *)styles {
_styles = [styles copy];
[self applyStyles];
}
- (void)setNativeAd:(GADNativeAd *)nativeAd {
((UILabel *)self.headlineView).text = nativeAd.headline;
// Some of the assets are not guaranteed to be present. This is to check
// that they are before showing or hiding them.
((UIImageView *)self.iconView).image = nativeAd.icon.image;
self.iconView.hidden = nativeAd.icon ? NO : YES;
[((UIButton *)self.callToActionView) setTitle:nativeAd.callToAction
forState:UIControlStateNormal];
// Either show the advertiser an app has, or show the store of the ad.
if (nativeAd.advertiser && !nativeAd.store) {
// Ad has advertiser but not store
self.storeView.hidden = YES;
((UILabel *)self.advertiserView).text = nativeAd.advertiser;
self.advertiserView.hidden = NO;
} else if (nativeAd.store && !nativeAd.advertiser) {
// Ad has store but not advertiser
self.advertiserView.hidden = YES;
((UILabel *)self.storeView).text = nativeAd.store;
self.storeView.hidden = NO;
} else if (nativeAd.advertiser && nativeAd.store) {
// Ad has both store and advertiser, default to showing advertiser.
self.storeView.hidden = YES;
((UILabel *)self.advertiserView).text = nativeAd.advertiser;
self.advertiserView.hidden = NO;
}
// Either show the number of stars an app has, or show the body of the ad.
// If there is a starRating then starRatingView is shown and bodyView is hidden
// otherwise, starRatingView is hidden and bodyView is filled.
// Use the unicode characters for filled in or empty stars.
if (nativeAd.starRating.floatValue > 0) {
NSMutableString* stars = [[NSMutableString alloc] initWithString:@""];
int count = 0;
for (; count < nativeAd.starRating.intValue; count++) {
NSString* filledStar = [NSString stringWithUTF8String:"\u2605"];
[stars appendString:filledStar];
}
for (; count < 5; count++) {
NSString* emptyStar = [NSString stringWithUTF8String:"\u2606"];
[stars appendString:emptyStar];
}
((UILabel *)self.starRatingView).text = stars;
self.bodyView.hidden = YES;
self.starRatingView.hidden = NO;
} else {
self.starRatingView.hidden = YES;
((UILabel *)self.bodyView).text = nativeAd.body;
self.bodyView.hidden = NO;
}
[self.mediaView setMediaContent:nativeAd.mediaContent];
[super setNativeAd:nativeAd];
}
- (void)addHorizontalConstraintsToSuperviewWidth {
// Add an autolayout constraint to make sure our template view stretches to fill the
// width of its parent.
if (self.superview) {
UIView* child = self;
[self.superview
addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:|[child]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(child)]];
}
}
- (void)addVerticalCenterConstraintToSuperview {
if (self.superview) {
UIView* child = self;
[self.superview addConstraint:[NSLayoutConstraint constraintWithItem:self.superview
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:child
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:0]];
}
}
/// Creates an opaque UIColor object from a byte-value color definition.
+ (UIColor*)colorFromHexString:(NSString*)hexString {
if (hexString == nil) {
return nil;
}
NSRange range = [hexString rangeOfString:@"^#[0-9a-fA-F]{6}$" options:NSRegularExpressionSearch];
if (range.location == NSNotFound) {
return nil;
}
unsigned rgbValue = 0;
NSScanner* scanner = [NSScanner scannerWithString:hexString];
[scanner setScanLocation:1]; // Bypass '#' character.
[scanner scanHexInt:&rgbValue];
return [UIColor colorWithRed:((rgbValue & 0xff0000) >> 16) / 255.0f
green:((rgbValue & 0xff00) >> 8) / 255.0f
blue:(rgbValue & 0xff) / 255.0f
alpha:1];
}
@end