繁体   English   中英

iOS 7通知中心与标签一样

[英]iOS 7 Notification Center Like Label

在iOS7通知中心,标签(和分隔线)有一个非常有趣的背景:模糊图像,以及看起来像柔和的光混合模式。

我不确定要搜索什么。 关于如何做到这一点的指针将非常感激。

直到现在,我试图通过使用label.textColor = [UIColor colorWithPatternImage:...]将模糊图像的一部分设置为背景来复制效果。 这也不考虑背景全黑(或白色)的情况,并导致文本变得不可读。

但这似乎并不合适。

像这样:

模糊的例子

这是我尝试过的:

- (void)viewDidLoad
{
    [super viewDidLoad];

    const CGFloat fontSize = 25.f;
    const NSString *text = @"A long-ish string";
    CGSize size = [text sizeWithAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"Avenir Next" size:fontSize]}];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(80, 270, size.width, size.height)];
    label.font = [UIFont fontWithName:@"Avenir Next" size:fontSize];
    label.textAlignment = NSTextAlignmentNatural;
    label.backgroundColor = [UIColor clearColor];
    label.text = text;

    UIImage *image = [UIImage imageNamed:@"wat@2x"];
    UIImage *blurredImage = [image applyBlurWithRadius:20.5 tintColor:[UIColor clearColor] saturationDeltaFactor:1.f maskImage:nil];

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[blurredImage applyDarkEffect]];
    imageView.frame = self.view.bounds;

    CGFloat imgScale = image.scale;
    CGRect labelFrame = label.frame;
    CGRect realRect = CGRectMake(labelFrame.origin.x * imgScale, labelFrame.origin.y * imgScale, labelFrame.size.width * imgScale, labelFrame.size.height * 2.0);
    CGImageRef labelPatternImage = CGImageCreateWithImageInRect(image.CGImage, realRect);
    label.textColor = [UIColor colorWithPatternImage:[UIImage imageWithCGImage:labelPatternImage scale:2.f orientation:UIImageOrientationUp]];
    CGImageRelease(labelPatternImage);

    [self.view addSubview:imageView];
    [self.view addSubview:label];
}

此代码生成代码结果http://caughtinflux.com/static/result.png
如您所见,这与NC标签不同。

编辑
文本的模糊图像背景应尽可能与实际背景对齐。 希望我的代码的模拟器截图有助于理解我所说的内容。

这是模糊效果。 您可以在UIImage上找到Apple的类别, 可以在此处下载此效果。 文件名是UIImage + ImageEffects.h / UIImage + ImageEffects.m你可以像这样使用它:

UIImage *backgImage = [image applyBlurWithRadius:2
tintColor:tintColor saturationDeltaFactor:0.8 maskImage:nil];

//扩展

您可以使用上面的标签创建视图,并使用白色文本颜色(在您将模糊整个视图时突出显示),之后您可以创建此视图的快照并将其设置为视图的背景use(by [self.view setBackgroundColor:[UIColor colorWithPatternImage:blurredImage]; )。

            UIView *snapshotView = [YOURUIVIEW resizableSnapshotViewFromRect:self.contentView.frame afterScreenUpdates:YES withCapInsets:UIEdgeInsetsZero];

            UIGraphicsBeginImageContextWithOptions( self.contentView.bounds.size, YES, 0.0f);
            BOOL result = [snapshotView drawViewHierarchyInRect:self.contentView.bounds
            afterScreenUpdates:YES];
            UIImage *snapshotImage =
            UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            if (result){
                UIColor *tintColor = [UIColor colorWithWhite:0.97 alpha:0.82];
                UIImage *blurredImage = [snapshotImage applyBlurWithRadius:4 tintColor:tintColor saturationDeltaFactor:1.8
                maskImage:nil];
            }

让我知道你需要的东西。 如果没有,你可以再次解释,更多细节,你想要实现什么?

您是否尝试过调整标签alpha值(听起来很简单)? 你可以尝试一下,也许可以在将模糊应用到标签之前为模糊添加一些白色。

我有一个带有通知中心的示例项目,如分隔符,选定的背景视图和(截至今天)标签。

您可以在这里查看: https//github.com/mikrohard/BluredTableView

可能仍然存在一些错误,性能问题等。但随意使用和改进它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM