How to replicate the blurred text in Notification Center (iOS 8)

前端 未结 5 1022
情深已故
情深已故 2020-12-04 18:27

I am playing with TodayExtension in iOS 8 and I wondered how to apply that blur effect to the Text or to buttons. I already figured out that it has something to do with UIVi

5条回答
  •  攒了一身酷
    2020-12-04 18:41

    The "Bearbeiten" button can be recreated by drawing a see through text on the button. How to do this has been answered in this post.

    Please see below for my solution. The original iOS "Bearbeiten" still looks a bit different. I've made it look similar by changing the alpha. Not the right solution. Hope that this post will trigger someone to find the correct way to do it (and share it).

    In viewDidLoad:

    _pinButton.backgroundColor = [UIColor clearColor];
    _pinButton.layer.cornerRadius = 4.0;
    _pinButton.clipsToBounds = YES;
    
    UIColor *white = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.75];
    UIColor *whiteT = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.3];
    UIFont *buttonFont = [UIFont fontWithName:@"HelveticaNeue" size:12.0];
    
    [_pinButton setImage:[self imageWithCutOutString:@"Pin" 
                                                size:_pinButton.frame.size 
                                     backgroundColor:white 
                                                font:buttonFont] 
                forState:UIControlStateNormal];
    
    [_pinButton setImage:[self imageWithCutOutString:@"Pin" 
                                                size:_pinButton.frame.size 
                                     backgroundColor:whiteT 
                                                font:buttonFont]
                forState:UIControlStateHighlighted];
    

    And the actual imageWithCutOutString method itself:

    - (UIImage *)imageWithCutOutString:(NSString *)string size:(CGSize)size backgroundColor:(UIColor *)backgroundColor font:(UIFont *)font
    {
        NSDictionary *textAttributes = @{ NSFontAttributeName:font };
    
        CGSize textSize = [string sizeWithAttributes:textAttributes];
    
        UIGraphicsBeginImageContextWithOptions(size, NO, [[UIScreen mainScreen] scale]);
        CGContextRef ctx = UIGraphicsGetCurrentContext();
    
        CGContextSetFillColorWithColor(ctx, backgroundColor.CGColor);
    
        UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0.0, 0.0, size.width, size.height)];
        CGContextAddPath(ctx, path.CGPath);
        CGContextFillPath(ctx);
    
        CGContextSetBlendMode(ctx, kCGBlendModeDestinationOut);
        CGPoint center = CGPointMake((size.width - textSize.width) / 2.0, (size.height - textSize.height) / 2.0);
        [string drawAtPoint:center withAttributes:textAttributes];
    
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
        UIGraphicsEndImageContext();
    
        return image;
    }
    

提交回复
热议问题