在superview.superview(sorting)透明的UILabel textColor

我想达到与iOS 7的新的iTunes远程更新相同的效果,用于我的UILabel

如果你看这个屏幕:

http://313e5987718b346aaf83-f5e825270f29a84f7881423410384342.r78.cf1.rackcdn.com/1383617182-2013-11-05_03.05.58_04.png

(右边的那个),你会发现UILabel文本的颜色是没有黑色蒙版的背景模糊的专辑封面。

目前我有透明的UILabel textColor( http://cl.ly/SInK ),我的代码类似于https://github.com/robinsenior/RSMaskedLabel

我的第一个假设是有一个像这样的视图层次结构

 UIImageView (Light blurred image) | UIImageView/UIView (Dark blurred image or just a dark mask for the superview) | UILabel (And all my other view). 

我想UILabel在第一个UIImageView上有一个透明的文字颜色,忽略第二个/ Mask)。

为了达到这个效果,我无法围绕解决scheme进行包装。

由于iOS 8苹果提供了一个新的类,UIVibrancyEffect,可以添加到UIVisualEffectView。 这与UIBlurEffect相结合,可以达到与我上面截图完全相同的结果。

来源: https : //developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIVibrancyEffect/index.html

我不认为你将能够直接与UILabel做到这一点。 我所做的是把它分解成几部分:

如何只绘制标签的背景。 为此,我将来自CGPathRef的代码从string转换为path。 然后,子类化UILabel并添加下面的drawRect做了适当的事情。

 -(void)drawRect:(CGRect)rect { CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextSaveGState(ctx); // Convert the attributed text to a UIBezierPath CGPathRef path = [self.attributedText createPath]; // Adjust the path bounds horizontally as requested by the view CGRect bounds = [self textRectForBounds:rect limitedToNumberOfLines:self.numberOfLines]; CGAffineTransform xform = CGAffineTransformMakeTranslation(bounds.origin.x, bounds.origin.y); // Adjust the path bounds vertically because it doesn't seem to be taken into account yet bounds = CGPathGetBoundingBox(path); xform = CGAffineTransformTranslate(xform, 0., (self.bounds.size.height - bounds.size.height) / 2.); // Apply the transform to the path path = CGPathCreateCopyByTransformingPath(path, &xform); // Set colors, the fill color should be the background color because we're going // to draw everything BUT the text, the text will be left clear. CGContextSetFillColorWithColor(ctx, self.textColor.CGColor); // Flip and offset things CGContextScaleCTM(ctx, 1., -1.); CGContextTranslateCTM(ctx, 0., 0. - self.bounds.size.height); // Invert the path CGContextAddRect(ctx, self.bounds); CGContextAddPath(ctx, path); CGContextDrawPath(ctx, kCGPathEOFill); // Discard the path CFRelease(path); // Restore gstate CGContextRestoreGState(ctx); } 

如果在界面生成器中创buildUILabel,请设置类,设置背景颜色清除,并将前景色设置为您提出的填充颜色,背景将显示文本的位置。

为了模糊显示标签的正文,我在标签下面放置了一个FXBlurView( https://github.com/nicklockwood/FXBlurView ),以确保它们alignment。

你可以在这里看到这个工作https://github.com/dwb357/TransparentLabel

颜色不统一。 但实际上是“切掉”,让模糊和有色的背景照耀。

一种方法是为glyphs构build一个CGPath,剪切graphics上下文,然后清除上下文,将区域保留在黑色之外,而区域内的glyths区域则是半透明的。

另一种方法是从这个大的模糊的有色封面艺术创build一个图案颜色,并在UILabel上使用这种图案颜色。