HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了MoPub如何使用具有动态单元格高度的MPTableViewAdPlacer实现iOS原生广告?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用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];
}

解决方法

您需要根据屏幕和桌面视图确定高度和宽度.我已经在运行时设置了所有组件并且运行良好.

我正在设置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,nil];
    [self.placer loadAdsForAdUnitID:kMoPubKey];
    [tableViewContent mp_setDatasource:self];
    [tableViewContent mp_setDelegate:self];
}

我为MoPubAd创建了tableviewcell.

@H_775_5@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;
@H_775_5@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,305 - 8 + 5);
        }
        else if (iPhone6)
        {
            viewBACkground.frame = CGRectMake(4,330 - 8 + 8);
        }
        else if (iPhone6Plus)
        {
            viewBACkground.frame = CGRectMake(4,355 - 8 );
        }
        [self addSubview:viewBACkground];
        self.iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10,10,48,48)];
//        self.iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(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,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,157)]; // 268
        }
        else if (iPhone6)
        {
            self.mainImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10,185)]; //320 260
        }
        else if (iPhone6Plus)
        {
            self.mainImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10,205)]; // 368
        }
        else
        {
            self.mainImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10,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);
}

我希望这对你有所帮助.

注意:无论使用哪种tableview方法,都要在ViewController中使用带有mp_前缀的方法.

大佬总结

以上是大佬教程为你收集整理的MoPub如何使用具有动态单元格高度的MPTableViewAdPlacer实现iOS原生广告?全部内容,希望文章能够帮你解决MoPub如何使用具有动态单元格高度的MPTableViewAdPlacer实现iOS原生广告?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。