objective-c iPhone UILabel 文字软阴影

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1709131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:29:19  来源:igfitidea点击:

iPhone UILabel text soft shadow

iphoneobjective-ciphone-sdk-3.0uilabelshadow

提问by Dimitris

I know soft shadows are not supported by the UILabel out of the box, on the iPhone. So what would be the best way to implement my own one?

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

EDIT:

编辑:

Obviously I will subclass the UILabeland draw in the -drawRect: My question is, how do I get the contents of the label as graphics and draw around them, blur them etc...

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

EDIT 2:

编辑2:

I returned to this question about a year later. In the meantime I've built a class that allows you to easily add soft shadow to a label and tweak it's radius etc and also to draw gradients on the text itself. You can find it on GitHub: https://github.com/doukasd/iOS-Components/tree/master/Views

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

采纳答案by Brad Larson

This answerto this similar questionprovides code for drawing a blurred shadow behind a UILabel. The author uses CGContextSetShadow() to generate the shadow for the drawn text.

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

回答by IlDan

As of 3.2 there is direct support for shadows in the SDK.

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

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

Import <QuartzCore/QuartzCore.h>and play with some parameters:

导入<QuartzCore/QuartzCore.h>并使用一些参数:

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

And, if you find your shadow clipped by the label bounds:

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

label.layer.masksToBounds = NO;

finally set

终于定了

label.layer.shouldRasterize = YES

回答by oksk

I advise you to use the shadowColor and shadowOffset properties of UILabel:

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

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

回答by Stefan

Additionally to IIDan's answer: For some purposes it is necessary to set

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

label.layer.shouldRasterize = YES

I think this is due to the blend mode that is used to render the shadow. For example I had a dark background and white text on it and wanted to "highlight" the text using a black shadowy glow. It wasn't working until I set this property.

我认为这是由于用于渲染阴影的混合模式。例如,我有一个深色背景和白色文本,并希望使用黑色阴影发光来“突出显示”文本。直到我设置了这个属性,它才起作用。

回答by Yang Meyer

Apply the (soft) shadow on the view's layer, like this:

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

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

回答by Sebastian Hojas

To keep things up to date: Creating the shadow in Swift is as easy as that:

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

Import the QuartzCore Framework

导入 QuartzCore 框架

import QuartzCore

And set the shadow attributes to your label

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

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

回答by bigjava

_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);

i think you should use the [UIColor colorWithWhite:0 alpha:0.2] to set the alpha value.

我认为您应该使用 [UIColor colorWithWhite:0 alpha:0.2] 来设置 alpha 值。

回答by Sean

I tried almost all of these techniques (except FXLabel) and couldn't get any of them to work with iOS 7. I did eventually find THLabel which is working perfectly for me. I used THLabel in Interface Builder and setup User Defined Runtime Attributes so that it's easy for a non programmer to control the look and feel.

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

https://github.com/MuscleRumble/THLabel

https://github.com/MuscleRumble/THLabel

回答by Nagarjun

This like a trick,

这就像一个伎俩,

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;

回答by Nick Lockwood

I wrote a library that provides a UILabel subclass with soft shadow support and a bunch of other effects:

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

https://github.com/nicklockwood/FXLabel

https://github.com/nicklockwood/FXLabel