iPhone UILabel文字软阴影

我知道UILabel不支持在iPhone上使用软阴影。 那么实现我自己的最好的方法是什么?

编辑:

很明显,我将inheritanceUILabel并在-drawRect中绘制:我的问题是,如何将标签的内容作为graphics获取并在它们周围绘制,模糊它们等等。

编辑2:

大约一年后我回到了这个问题。 与此同时,我创build了一个类,允许您轻松地将柔和的阴影添加到标签,并调整其半径等,也可以在文本本身上绘制渐变。 你可以在GitHub上find它: https : //github.com/doukasd/iOS-Components/tree/master/Views

这个类似问题的 答案提供了在UILabel后面绘制模糊阴影的代码。 作者使用CGContextSetShadow()为绘制的文本生成阴影。

从3.2开始直接支持SDK中的阴影。

 label.layer.shadowColor = [label.textColor CGColor]; label.layer.shadowOffset = CGSizeMake(0.0, 0.0); 

导入<QuartzCore/QuartzCore.h>并播放一些参数:

 label.layer.shadowRadius = 3.0; label.layer.shadowOpacity = 0.5; 

而且,如果你发现你的影子被标签边界限制:

 label.layer.masksToBounds = NO; 

终于设定

 label.layer.shouldRasterize = YES 

我build议你使用UILabel的shadowColor和shadowOffset属性:

 UILabel* label = [[UILabel alloc] init]; label.shadowColor = [UIColor whiteColor]; label.shadowOffset = CGSizeMake(0,1); 

除了IIDan的回答:为了某些目的,有必要设置

 label.layer.shouldRasterize = YES 

我认为这是由于用来渲染阴影的混合模式。 例如,我有一个黑色的背景和白色的文字,并希望用黑色的阴影辉光“突出显示”文字。 这是不工作,直到我设置此属性。

在视图的图层上应用(软)阴影,如下所示:

 UILabel *label = [[UIabel alloc] init]; label.layer.shadowColor = [[UIColor whiteColor] CGColor]; label.layer.shadowOpacity = 1.0; 

让事情保持最新:在Swift中创build阴影就像这样简单:

导入QuartzCore框架

 import QuartzCore 

并将阴影属性设置为您的标签

 titleLabel.shadowColor = UIColor.blackColor() titleLabel.shadowOffset = CGSizeMake(0.0, 0.0) titleLabel.layer.shadowRadius = 5.0 titleLabel.layer.shadowOpacity = 0.8 titleLabel.layer.masksToBounds = false titleLabel.layer.shouldRasterize = true 

我尝试了几乎所有这些技术(FXLabel除外),并没有得到任何与iOS 7的工作。我终于find了THLabel这是完美的工作。 我在界面生成器中使用了THLabel,并设置了用户定义的运行时属性,这样非编程人员就很容易控制外观。

https://github.com/MuscleRumble/THLabel

这就像一个把戏,

 UILabel *customLabel = [[UILabel alloc] init]; UIColor *color = [UIColor blueColor]; customLabel.layer.shadowColor = [color CGColor]; customLabel.layer.shadowRadius = 5.0f; customLabel.layer.shadowOpacity = 1; customLabel.layer.shadowOffset = CGSizeZero; customLabel.layer.masksToBounds = NO; 
 _nameLabel = [[UILabel alloc] initWithFrame:CGRectZero]; _nameLabel.font = [UIFont boldSystemFontOfSize:19.0f]; _nameLabel.textColor = [UIColor whiteColor]; _nameLabel.backgroundColor = [UIColor clearColor]; _nameLabel.shadowColor = [UIColor colorWithWhite:0 alpha:0.2]; _nameLabel.shadowOffset = CGSizeMake(0, 1); 

我想你应该使用[UIColor colorWithWhite:0 alpha:0.2]来设置alpha值。

我写了一个库,提供了软阴影支持和一堆其他效果的UILabel子类:

https://github.com/nicklockwood/FXLabel

如上所述,子类UILabel,然后,在drawRect :, do [self drawTextInRect:rect]; 将文本绘制到当前上下文中。 一旦它在那里,你可以开始使用它添加filter和什么。 如果你想用刚刚绘制的上下文制作阴影,你应该可以使用:

 CGContextSetShadowWithColor() 

在文档中查看这个函数来学习如何使用它。

从iOS 5开始,Apple提供了一种私有API方法来创build具有柔和阴影的标签。 标签速度非常快:我在一系列透明的视图中同时使用了几十个,滚动animation没有减速。

这只适用于非App Store应用程序(显然),您需要头文件。

 $SBBulletinBlurredShadowLabel = NSClassFromString("SBBulletinBlurredShadowLabel"); CGRect frame = CGRectZero; SBBulletinBlurredShadowLabel *label = [[[$SBBulletinBlurredShadowLabel alloc] initWithFrame:frame] autorelease]; label.backgroundColor = [UIColor clearColor]; label.textColor = [UIColor whiteColor]; label.font = [UIFont boldSystemFontOfSize:12]; label.text = @"I am a label with a soft shadow!"; [label sizeToFit]; 

Swift 3 ,你可以创build一个扩展:

 import UIKit extension UILabel { func shadow() { self.layer.shadowColor = self.textColor.cgColor self.layer.shadowOffset = CGSize.zero self.layer.shadowRadius = 3.0 self.layer.shadowOpacity = 0.5 self.layer.masksToBounds = false self.layer.shouldRasterize = true } } 

并通过以下方式使用:

 label.shadow() 

子类UILabel,并覆盖-drawInRect: