59

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

编辑:

显然,我将子类化UILabel并在 -drawRect 中绘制:我的问题是,如何将标签的内容作为图形获取并在它们周围绘制,模糊它们等......

编辑2:

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

4

15 回答 15

191

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

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

#import <QuartzCore/QuartzCore.h>并使用一些参数:

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

而且,如果你发现你的阴影被标签边界剪掉了:

label.layer.masksToBounds = NO;

终于定了

label.layer.shouldRasterize = YES;
于 2011-01-20T14:29:42.140 回答
38

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

UILabel* label = [[UILabel alloc] init];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0,1);
于 2010-03-02T13:15:01.957 回答
19

This answer to this similar question提供了在 UILabel 后面绘制模糊阴影的代码。作者使用 CGContextSetShadow() 为绘制的文本生成阴影。

于 2009-11-10T18:19:39.043 回答
17

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

label.layer.shouldRasterize = YES

我认为这是由于用于渲染阴影的混合模式。例如,我有一个深色背景和白色文本,并想使用黑色阴影辉光“突出显示”文本。在我设置这个属性之前它没有工作。

于 2013-03-20T16:31:00.633 回答
7

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

UILabel *label = [[UIabel alloc] init];
label.layer.shadowColor = [[UIColor whiteColor] CGColor];
label.layer.shadowOpacity = 1.0;
于 2010-05-19T09:45:06.840 回答
7

保持最新:在 Swift 中创建阴影就这么简单:

导入 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
于 2015-08-19T11:14:37.437 回答
5
_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 值。

于 2013-07-22T03:17:04.370 回答
4

我尝试了几乎所有这些技术(FXLabel 除外),但无法让它们中的任何一个与 iOS 7 一起使用。我最终发现 THLabel 非常适合我。我在 Interface Builder 中使用了 THLabel 并设置了用户定义的运行时属性,以便非程序员可以轻松控制外观。

https://github.com/MuscleRumble/THLabel

于 2013-11-22T16:03:01.853 回答
4

这就像一个把戏,

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;
于 2015-01-09T07:39:40.127 回答
3

子类 UILabel,如前所述,然后,在 drawRect: 中[self drawTextInRect:rect];,将文本绘制到当前上下文中。一旦它在那里,您可以通过添加过滤器和诸如此类的东西开始使用它。如果您想用刚刚在上下文中绘制的内容制作阴影,您应该能够使用:

CGContextSetShadowWithColor()

在文档中查看该功能以了解如何使用它。

于 2009-11-10T18:20:04.347 回答
3

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

https://github.com/nicklockwood/FXLabel

于 2012-06-07T09:20:39.903 回答
3

Swift 3中,您可以创建一个扩展:

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()
于 2017-01-05T08:00:41.503 回答
1

从 iOS 5 开始,Apple 提供了一种私有 api 方法来创建带有软阴影的标签。标签非常快:我在一系列透明视图中同时使用几十个标签,并且滚动动画没有减慢。

这仅对非 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];
于 2011-10-05T22:43:57.847 回答
0

虽然不可能直接在 上设置模糊半径UILabel,但您绝对可以通过操纵 CALayer 来更改它。

只需设置:

//Required properties
customLabel.layer.shadowRadius = 5.0 //set shadow radius to your desired value.
customLabel.layer.shadowOpacity = 1.0 //Choose an opacity. Make sure it's visible (default is 0.0)

//Other options
customLabel.layer.shadowOffset = CGSize(width: 10, height: 10)
customLabel.layer.shadowColor = UIColor.black.cgColor
customLabel.layer.masksToBounds = false

我希望能帮助某人和其他答案未能澄清的是,如果您在尝试设置时还直接在 Interface Builder 上设置 UILabel属性,它将不起作用Shadow Color.layer.shadowRadius

因此,如果设置label.layer.shadowRadius不起作用,请Shadow ColorUILabelInterface Builder 上进行验证。它应该设置为default。然后,如果您想要黑色以外的阴影颜色,请也通过.layer属性设置此颜色。

在此处输入图像描述

于 2020-06-16T20:52:54.927 回答
-3

子类 UILabel,并覆盖 -drawInRect:

于 2009-11-10T16:15:54.497 回答