Ios 如何模糊UILabel文本

Ios 如何模糊UILabel文本,ios,uilabel,Ios,Uilabel,我试图在没有图像模糊方法的情况下模糊UILabel的文本。我找到了很多解决方案,但最大的是图像。我尝试了图层的阴影功能,但没有帮助 您只需创建UILabel的子类即可创建模糊效果 以下是锅炉模板: 模糊UILabel.h #import <UIKit/UIKit.h> @interface BlurredUILabel : UILabel @property (nonatomic, readwrite) IBInspectable CGFloat blurRadius; @en

我试图在没有图像模糊方法的情况下模糊
UILabel
的文本。我找到了很多解决方案,但最大的是图像。我尝试了图层的阴影功能,但没有帮助

您只需创建UILabel的子类即可创建模糊效果

以下是锅炉模板:

模糊UILabel.h

#import <UIKit/UIKit.h>

@interface BlurredUILabel : UILabel

@property (nonatomic, readwrite) IBInspectable CGFloat blurRadius;

@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.h

#import <UIKit/UIKit.h>

@interface BlurredUILabel : UILabel

@property (nonatomic, readwrite) IBInspectable CGFloat blurRadius;

@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


使用此代码检查此答案。谢谢Utasav,但我不想使用这种东西,因为那可能是沉重的表格单元格。在标签上放置一个模糊图像。@RaviGautam-在iOS 8中,您可以使用
UIBlurEffect
UIVisualEffectView
来有效地模糊标签。但这并不能控制模糊的程度(而且
UIBlurEffect
非常极端)。如果你想对模糊进行更多的控制,那么你就必须做一些基于图像的模糊处理,比如核心图像(正如Lefteris所建议的那样(这在计算上非常密集)或者使用WWDC 2013中的
iOS\u UIImageEffects
。请参阅使用此代码模糊UILabel检查此答案。感谢Utasav,但我不想使用此类内容,因为这可能是沉重的表格单元格。在标签上放置模糊图像。@RaviGautam-在iOS 8中,您可以使用
UIBlurEffect
UIVisualEffectView
来提高效率模糊标签。但这不允许你控制模糊的程度(而且
UIBlurEffect
非常极端)。如果你想对模糊进行更多的控制,那么你就不得不做一些基于图像的模糊处理,比如核心图像(如Lefteris所建议的(这在计算上非常密集)或者使用WWDC 2013中的
iOS_UIImageEffects
。谢谢Lefteris,我将尝试此解决方案。顺便说一句,使用核心图像进行模糊时要小心,注意结果图像比原始图像大。这可能会导致在
设置内容时模糊图像发生不希望的偏移。使用可以防止这种情况调用
createCGImage
时,ode>[imageToBlur extent]
(或者在
[outputImage extent]
中创建大小合适的居中
CGRect
).谢谢Lefteris,有没有办法回滚标签的状态?我不知道..可能会从层中删除cgimg。内容?!我只会在子类中添加一个布尔实例变量作为标志,当它设置为“是”时,我不会在setText函数中进行模糊…然后我会在设置不模糊的文本之前打开标志当我想再次模糊时,请将其关闭。这不起作用。这里有人运气好吗?谢谢Lefteris,我将尝试此解决方案。顺便说一句,使用核心图像进行模糊时要小心,注意结果图像比原始图像大。这可能会导致在
设置内容时模糊图像发生不希望的移动。可以在调用
createCGImage
时使用
[imagestoblur extent]
(或者在
[outputImage extent]
中创建正确大小的居中
CGRect
来防止这种情况发生).谢谢Lefteris,有没有办法回滚标签的状态?我不知道..可能会从层中删除cgimg。内容?!我只会在子类中添加一个布尔实例变量作为标志,当它设置为“是”时,我不会在setText函数中进行模糊…然后我会在设置不模糊的文本之前打开标志当我想要模糊的时候把它关掉。这不起作用。这里有人运气好吗?