ios 以编程方式向 UIButton 标签添加阴影

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5178896/
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-08-30 19:06:37  来源:igfitidea点击:

Programmatically adding a shadow to a UIButton label

iosuibuttonios-4.2

提问by edhog

I'm trying to add a 1px black drop shadow to a button label with no luck. I've tried this:self.setTitleShadowOffset = CGSizeMake(0, -1);but I get:

我试图在没有运气的情况下向按钮标签添加 1px 黑色阴影。我试过这个:self.setTitleShadowOffset = CGSizeMake(0, -1);但我得到:

Request for member 'setTitleShadowOffset' in something not a structure or union

在不是结构或联合的东西中请求成员“setTitleShadowOffset”

Any suggestions would be fantastic thanks!

任何建议都会很棒谢谢!

回答by fsaint

The right property is self.titleLabel.shadowOffset:

正确的属性是 self.titleLabel.shadowOffset:

UIButton *b = [UIButton buttonWithType:UIButtonTypeRoundedRect];    
[b setTitleShadowColor:[UIColor purpleColor] forState:UIControlStateNormal];
b.titleLabel.shadowOffset = CGSizeMake(1.0, 1.0);
[b setTitle:@"Hello, I'm a Button" forState:UIControlStateNormal];
b.frame = CGRectMake(10.0, 10.0,300.0, 40.0);

回答by ArtOfWarfare

The other answers do not properly set the shadow color (I suspect they didn't notice because they were trying to set the shadow color to what it is by default, black.)

其他答案没有正确设置阴影颜色(我怀疑他们没有注意到,因为他们试图将阴影颜色设置为默认情况下的黑色。)

This code worked for me to add a white shadow to the text of my button:

这段代码对我有用,可以为我的按钮文本添加一个白色阴影:

myButton.titleLabel.shadowOffset = CGSizeMake(0, 1);
[myButton setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal];

回答by visakh7

The setTitleShadowOffsetfor UIButtonis deprecated. Use the shadowOffsetof titleLabel property of UIButton

setTitleShadowOffsetUIButton已被弃用。使用的shadowOffsettitleLabel 属性UIButton

buttonName.titleLabel.shadowOffset = CGSizeMake(0, -1);

buttonName.titleLabel.shadowOffset = CGSizeMake(0, -1);

回答by Raphael Souza

In Swift 3.0

在 Swift 3.0 中

myButton.titleLabel?.layer.shadowRadius = 3
myButton.titleLabel?.layer.shadowColor = UIColor.black.cgColor
myButton.titleLabel?.layer.shadowOffset = CGSize(width: 0, height: 1)
myButton.titleLabel?.layer.shadowOpacity = 0.5
myButton.titleLabel?.layer.masksToBounds = false

enter image description here

在此处输入图片说明

回答by Wilson

for Swift 3:

对于 Swift 3:

  button.setTitleShadowColor(UIColor.red, for: .normal)
  button.titleLabel?.shadowOffset = CGSize(width: 2, height: 2)

回答by Userich

Here is how to add shadow to the button title in Objective-C with radius property:

以下是如何使用半径属性为 Objective-C 中的按钮标题添加阴影:

#import <QuartzCore/QuartzCore.h>    

button.titleLabel.layer.shadowOffset = CGSizeMake(2.0, 2.0);
button.titleLabel.layer.shadowColor = [UIColor colorWithWhite:0.1 alpha:0.7].CGColor;
button.titleLabel.layer.shadowRadius = 2.0;
button.titleLabel.layer.shadowOpacity = 1.0;
button.titleLabel.layer.masksToBounds = NO;