Jac*_*ang 3 objective-c ios mopub facebook-audience-network
我正在使用MPTableViewAdPlacer在iOS UITableView中实现原生广告.初始化MPTableViewAdPlacer时,它会要求MPStaticNativeAdRendererSettings,它需要实现viewSizeHandler.但是,这是在提取任何广告之前,因为名称建议为"静态"原生广告.我正在尝试实现一个可以在获取adData后计算单元格高度,例如标题,图像等.我一直试图找到一种方法来实现动态单元格高度,但所有示例应用程序,twitter提供的说明仅显示静态高度实现.
代码如下:
-(void)setupAdPlacer {
MPNativeAdRequestTargeting *targeting = [MPNativeAdRequestTargeting targeting];
targeting.location = [[CLLocationManager alloc] init].location;
targeting.desiredAssets = [NSSet setWithObjects: kAdMainImageKey, kAdCTATextKey, kAdTextKey, kAdTitleKey, nil];
MPStaticNativeAdRendererSettings *settings = [[MPStaticNativeAdRendererSettings alloc] init];
settings.renderingViewClass = [REPostListViewMoPubAdCell class];
settings.viewSizeHandler = ^(CGFloat maximumWidth) {
return CGSizeMake(maximumWidth, 312.0);
// STATIC HEIGHT
};
MPNativeAdRendererConfiguration *config = [MPStaticNativeAdRenderer rendererConfigurationWithRendererSettings:settings];
self.adPlacer = [MPTableViewAdPlacer placerWithTableView:self.tableView viewController:self adPositioning:positioning rendererConfigurations:@[config]];
self.adPlacer.delegate = self;
[self.adPlacer loadAdsForAdUnitID:@"xxxxxxxxxxx" targeting:targeting];
}
Run Code Online (Sandbox Code Playgroud)
您需要根据屏幕和桌面视图确定高度和宽度.我已经在运行时设置了所有组件并且运行良好.
我正在设置mopub使用.
-(void)setUpMopPubAd
{
MPServerAdPositioning *positioning = [[MPServerAdPositioning alloc] init];
self.placer = [MPTableViewAdPlacer placerWithTableView:tableViewContent viewController:self adPositioning:positioning defaultAdRenderingClass:[MoPubAdTableViewCell class]];
MPNativeAdRequestTargeting *targeting = [MPNativeAdRequestTargeting targeting]; targeting.desiredAssets = [NSSet setWithObjects:kAdIconImageKey, kAdMainImageKey, kAdCTATextKey, kAdTextKey, kAdTitleKey, nil];
[self.placer loadAdsForAdUnitID:kMoPubKey];
[tableViewContent mp_setDataSource:self];
[tableViewContent mp_setDelegate:self];
}
Run Code Online (Sandbox Code Playgroud)
我为MoPubAd创建了tableviewcell.
MoPubAdTableViewCell.h
@interface MoPubAdTableViewCell : UITableViewCell<MPNativeAdRendering>
@property (strong, nonatomic) IBOutlet UILabel *titleLabel;
@property (strong, nonatomic) IBOutlet UILabel *mainTextLabel;
@property (strong, nonatomic) IBOutlet UIButton *callToActionButton;
@property (strong, nonatomic) IBOutlet UIImageView *iconImageView;
@property (strong, nonatomic) IBOutlet UIImageView *mainImageView;
Run Code Online (Sandbox Code Playgroud)
MoPubAdTableViewCell.m
@synthesize titleLabel, mainImageView, iconImageView, mainTextLabel,callToActionButton;
- (void)awakeFromNib
{
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
UIView *viewBackground = [[UIView alloc]init];
[viewBackground.layer setMasksToBounds:YES];
[viewBackground.layer setBorderWidth:1.0];
[viewBackground.layer setBorderColor:[[UIColor colorWithRed:165.0/255.0 green:166.0/255.0 blue:167.0/255.0 alpha:1.0]CGColor]];
[viewBackground setBackgroundColor:[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:0.2]];
if (iPhone4)
{
viewBackground.frame = CGRectMake(4, 4, [UIScreen mainScreen].bounds.size.width - 8 , 305 - 8 + 5);
}
else if (iPhone5)
{
viewBackground.frame = CGRectMake(4, 4, [UIScreen mainScreen].bounds.size.width - 8 , 305 - 8 + 5);
}
else if (iPhone6)
{
viewBackground.frame = CGRectMake(4, 4, [UIScreen mainScreen].bounds.size.width - 8 , 330 - 8 + 8);
}
else if (iPhone6Plus)
{
viewBackground.frame = CGRectMake(4, 4, [UIScreen mainScreen].bounds.size.width - 8 , 355 - 8 );
}
[self addSubview:viewBackground];
self.iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 48, 48)];
// self.iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 60, 60)];
[self.iconImageView.layer setMasksToBounds:YES];
self.iconImageView.layer.cornerRadius = 5.0;
[self addSubview:self.iconImageView];
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 10, 252, 25)];
[self.titleLabel setFont:[UIFont fontWithName:@"Roboto-Bold" size:15.0]];
[self.titleLabel setText:@"Title"];
[self.titleLabel setBackgroundColor:[UIColor clearColor]];
[self.titleLabel setAdjustsFontSizeToFitWidth:YES];
[self addSubview:self.titleLabel];
UILabel *lblSponsored = [[UILabel alloc]initWithFrame:CGRectMake(self.titleLabel.frame.origin.x, self.titleLabel.frame.origin.y + self.titleLabel.frame.size.height , self.titleLabel.frame.size.width, 20)];
lblSponsored.text = @"Sponsored";
lblSponsored.font = [UIFont fontWithName:@"Roboto-Regular" size:13.0];
[lblSponsored setTextAlignment:NSTextAlignmentLeft];
lblSponsored.textColor = [UIColor lightGrayColor];
[lblSponsored setBackgroundColor:[UIColor clearColor]];
[self addSubview:lblSponsored];
self.mainTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 65, [UIScreen mainScreen].bounds.size.width - 20, 35)];
[self.mainTextLabel setFont:[UIFont systemFontOfSize:14.0f]];
[self.mainTextLabel setBackgroundColor:[UIColor clearColor]];
[self.mainTextLabel setText:@"Text"];
[self.mainTextLabel setNumberOfLines:2];
[self addSubview:self.mainTextLabel];
self.mainImageView.contentMode = UIViewContentModeScaleAspectFit;
if (iPhone4 || iPhone5)
{
self.mainImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, self.mainTextLabel.frame.size.height + self.mainTextLabel.frame.origin.y + 5, [UIScreen mainScreen].bounds.size.width - 20, 157)]; // 268
}
else if (iPhone6)
{
self.mainImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, self.mainTextLabel.frame.size.height + self.mainTextLabel.frame.origin.y + 5, [UIScreen mainScreen].bounds.size.width - 20, 185)]; //320 260
}
else if (iPhone6Plus)
{
self.mainImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, self.mainTextLabel.frame.size.height + self.mainTextLabel.frame.origin.y + 5, [UIScreen mainScreen].bounds.size.width - 20, 205)]; // 368
}
else
{
self.mainImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, self.mainTextLabel.frame.size.height + self.mainTextLabel.frame.origin.y + 5, [UIScreen mainScreen].bounds.size.width - 20, 157)];
}
[self.mainImageView setClipsToBounds:YES];
// [self.mainImageView setContentMode:UIViewContentModeScaleAspectFill];
[self addSubview:self.mainImageView];
self.callToActionButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.callToActionButton setFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 105, self.mainImageView.frame.origin.y + self.mainImageView.frame.size.height + 5, 95, 30)];
[self.callToActionButton.titleLabel setAdjustsFontSizeToFitWidth:YES];
[self.callToActionButton.layer setMasksToBounds:YES];
// [self.callToActionButton.layer setBorderWidth:1.0];
[self.callToActionButton.layer setCornerRadius:5.0];
// [self.callToActionButton.layer setBorderColor:[[UIColor colorWithRed:165.0/255.0 green:166.0/255.0 blue:167.0/255.0 alpha:1.0]CGColor]];
[self.callToActionButton setBackgroundColor:[UIColor colorWithRed:0.0/255.0 green:158.0/255.0 blue:255.0/255.0 alpha:1.0]];
self.callToActionButton.userInteractionEnabled = NO;
[self addSubview:self.callToActionButton];
self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5f];
self.titleLabel.textColor = [UIColor colorWithWhite:0.86 alpha:1.0f];
self.mainTextLabel.textColor = [UIColor colorWithWhite:0.86 alpha:1.0f];
self.titleLabel.font = [UIFont fontWithName:@"Roboto-Bold" size:15.0];
self.mainTextLabel.font = [UIFont fontWithName:@"Roboto-Regular" size:13.0];
}
return self;
}
- (void)layoutAdAssets:(MPNativeAd *)adObject
{
[adObject loadTitleIntoLabel:self.titleLabel];
[adObject loadTextIntoLabel:self.mainTextLabel];
[adObject loadCallToActionTextIntoButton:self.callToActionButton];
// [adObject loadCallToActionTextIntoLabel:self.callToActionButton.titleLabel];
[adObject loadIconIntoImageView:self.iconImageView];
[adObject loadImageIntoImageView:self.mainImageView];
}
+ (CGSize)sizeWithMaximumWidth:(CGFloat)maximumWidth
{
if (iPhone4 || iPhone5)
{
return CGSizeMake(maximumWidth, 305 + 5);
}
else if (iPhone6)
{
return CGSizeMake(maximumWidth, 330 + 8);
}
else
{
return CGSizeMake(maximumWidth, 355 );
}
return CGSizeMake(maximumWidth, 295 + 15);
}
Run Code Online (Sandbox Code Playgroud)
我希望这对你有所帮助.
注意:无论使用哪种tableview方法,都要mp_在ViewController中使用带前缀的方法.
| 归档时间: |
|
| 查看次数: |
1008 次 |
| 最近记录: |