如何模糊UILabel文本 [英] How to blur UILabel text
本文介绍了如何模糊UILabel文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图模糊 UILabel
的文字而没有图像模糊方法。我发现了许多解决方案,但最大的是图像。我尝试了图层的阴影功能,但这没有用。
I am trying to blur the text of UILabel
without image blurring methods. I found many solutions but maximum are there for images. I tried shadow functionality of layer but that is not helpful.
推荐答案
你可以创建一个UILabel的子类来创建模糊效果。
You can just create a subclass of UILabel that creates the blur effect.
这是一个锅炉模板:
BlurredUILabel.h
BlurredUILabel.h
#import <UIKit/UIKit.h>
@interface BlurredUILabel : UILabel
@property (nonatomic, readwrite) IBInspectable CGFloat blurRadius;
@end
BlurredUILabel.m
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
你会像使用它一样这个:
And you would use it like this:
#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文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文