如何模糊 UILabel 文本
How to blur UILabel text
我正在尝试不使用图像模糊方法来模糊 UILabel
的文本。我找到了很多解决方案,但最多的是图像。我尝试了图层的阴影功能,但这没有帮助。
您可以创建一个 UILabel 的子类来创建模糊效果。
这是一个锅炉模板:
BlurredUILabel.h
#import <UIKit/UIKit.h>
@interface BlurredUILabel : UILabel
@property (nonatomic, readwrite) IBInspectable CGFloat blurRadius;
@end
BlurredUILabel.m
#import "BlurredUILabel.h"
@implementation BlurredUILabel
- (void)setText:(NSString *)text {
super.text = text;
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
[blurFilter setDefaults];
CIImage *imageToBlur = [CIImage imageWithCGImage:image.CGImage];
[blurFilter setValue:imageToBlur forKey:kCIInputImageKey];
[blurFilter setValue:@(self.blurRadius) forKey:@"inputRadius"];
CIImage *outputImage = blurFilter.outputImage;
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
[self.layer setContents:(__bridge id)cgimg];
CGImageRelease(cgimg);
}
@end
你会像这样使用它:
#import "TestViewController.h"
#import "BlurredUILabel.h"
@interface TestViewController ()
@end
@implementation TestViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
BlurredUILabel *label = [[BlurredUILabel alloc] initWithFrame:CGRectMake(50, 50, 300, 50)];
label.blurRadius = 2.0;
label.backgroundColor = [UIColor redColor];
label.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:35];
label.textAlignment = NSTextAlignmentCenter;
label.text = @"HELLO";
[self.view addSubview:label];
label.blurRadius = 1.5;
[label performSelector:@selector(setText:) withObject:@"Test new string" afterDelay:2];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
我正在尝试不使用图像模糊方法来模糊 UILabel
的文本。我找到了很多解决方案,但最多的是图像。我尝试了图层的阴影功能,但这没有帮助。
您可以创建一个 UILabel 的子类来创建模糊效果。
这是一个锅炉模板:
BlurredUILabel.h
#import <UIKit/UIKit.h>
@interface BlurredUILabel : UILabel
@property (nonatomic, readwrite) IBInspectable CGFloat blurRadius;
@end
BlurredUILabel.m
#import "BlurredUILabel.h"
@implementation BlurredUILabel
- (void)setText:(NSString *)text {
super.text = text;
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
[blurFilter setDefaults];
CIImage *imageToBlur = [CIImage imageWithCGImage:image.CGImage];
[blurFilter setValue:imageToBlur forKey:kCIInputImageKey];
[blurFilter setValue:@(self.blurRadius) forKey:@"inputRadius"];
CIImage *outputImage = blurFilter.outputImage;
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
[self.layer setContents:(__bridge id)cgimg];
CGImageRelease(cgimg);
}
@end
你会像这样使用它:
#import "TestViewController.h"
#import "BlurredUILabel.h"
@interface TestViewController ()
@end
@implementation TestViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
BlurredUILabel *label = [[BlurredUILabel alloc] initWithFrame:CGRectMake(50, 50, 300, 50)];
label.blurRadius = 2.0;
label.backgroundColor = [UIColor redColor];
label.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:35];
label.textAlignment = NSTextAlignmentCenter;
label.text = @"HELLO";
[self.view addSubview:label];
label.blurRadius = 1.5;
[label performSelector:@selector(setText:) withObject:@"Test new string" afterDelay:2];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end