UILabel与半透明/模糊背景视图

时间:2014-12-10 18:55:12

标签: ios objective-c

我在Storyboard上创建了UILabel。实际上它有一个白色背景,但我想让它变得更加狡猾。有可能吗?它只有一个与背景相关的属性backgroundColor,所以我不知道应该如何实现它。

3 个答案:

答案 0 :(得分:3)

要在iOS 8中制作模糊的背景视图,您可以使用UIVisualEffectView和UIVisualEffect:

UIVisualEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:effect];
blurEffectView.frame = self.view.frame;
[blueEffectView addSubview:*your label*];
[self.view addSubview:self.blurEffectView];

对于< iOS 8还有另一种选择。您可以使用Apple class" UIImage + ImageEffects.h"。逻辑是一样的,但在你需要制作屏幕截图之前,所以它看起来像这样:

- (void)applyBlur
{
    UIImage *screen = [self captureScreenInRect:self.view.bounds];
    UIImage *blur = [screen applyDarkEffect];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.frame];
    imageView.image = blur;
    [self.view addSubview:imageView];
}

- (UIImage *)captureScreenInRect:(CGRect)captureFrame 
{
        CALayer *layer;
        layer = self.view.layer;
        UIGraphicsBeginImageContext(self.view.bounds.size);
        CGContextClipToRect (UIGraphicsGetCurrentContext(),captureFrame);
        [layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return screenImage;
 }

答案 1 :(得分:0)

label.backgroundColor = [UIColor clearColor];

答案 2 :(得分:0)

由于你想支持iOS 7-8,你可以使用FXBlurView.它在两者上都非常棒,你可以在故事板上使用它。此外,如果您想利用Xcode 6 IBDesignable Classes,您可以启用通过Storyboard配置视图的功能。

<强> FXBlurView.h

IB_DESIGNABLE
@interface FXBlurView : UIView

+ (void)setBlurEnabled:(BOOL)blurEnabled;
+ (void)setUpdatesEnabled;
+ (void)setUpdatesDisabled;

@property (nonatomic, getter = isBlurEnabled) IBInspectable BOOL blurEnabled;
@property (nonatomic, getter = isDynamic) IBInspectable BOOL dynamic;
@property (nonatomic, assign) IBInspectable NSUInteger iterations;
@property (nonatomic, assign) IBInspectable NSTimeInterval updateInterval;
@property (nonatomic, assign) IBInspectable CGFloat blurRadius;
@property (nonatomic, strong) IBInspectable UIColor *tintColor;
@property (nonatomic, weak_ref) UIView *underlyingView;

- (void)updateAsynchronously:(BOOL)async completion:(void (^)())completion;

@end

注意:即使FXBlurView在iOS 8上运行正常,我也建议您使用原生的UIVisualEffect&amp; UIVisualEffectView,正如@gronzzz所建议的,适用于iOS 8,因为这些类的设计与低性能成本完全相同。

Ray Wenderlich iOS 8 Visual Effect Tutorial