Programmatically adding a shadow to a UIButton label
Asked Answered
T

6

19

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:

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

Any suggestions would be fantastic thanks!

Telemechanics answered 3/3, 2011 at 9:36 Comment(0)
S
23

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];
Streaky answered 1/1, 2013 at 18:47 Comment(4)
Setting myKey.titleLabel.shadowColor is not working indeed!Agape
You can't set that using properties - you need to set it using the full method because otherwise you're leaving off forState (meaning when the state changes, it'll lose the color you set.) The code snippet I provided should work.Streaky
This is the best solution so far, just wanted to point that the proper way of doing this is using CGSizeMake not CGSizeChecklist
Kind of funny - 3 years after I posted this, and nearly 5 years after the question was asked, it finally got marked as the correct answer.Streaky
I
37

The right property is 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);
Incorporation answered 3/3, 2011 at 9:41 Comment(3)
The shadowColor line isn't working as you think it is. You need to use setShadowColor:forState: (you won't notice the failure using this example because it sets the shadow color to black, which is the default.)Streaky
Thanks @ArtOfWarfare. Haven't tried this in iOS 6 and the behavior may have changed. I'll test and update the code.Incorporation
@Felz, it never behaved the way you thought it did. Shadow colors have always been stately, much like other color properties throughout iOS, meaning that they reset to their default color as soon as their state changes (IE, because they're tapped or highlighted) unless you use their setColor:forState: methods. You just didn't notice it in this particular case because the default color of shadows is black.Streaky
S
23

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];
Streaky answered 1/1, 2013 at 18:47 Comment(4)
Setting myKey.titleLabel.shadowColor is not working indeed!Agape
You can't set that using properties - you need to set it using the full method because otherwise you're leaving off forState (meaning when the state changes, it'll lose the color you set.) The code snippet I provided should work.Streaky
This is the best solution so far, just wanted to point that the proper way of doing this is using CGSizeMake not CGSizeChecklist
Kind of funny - 3 years after I posted this, and nearly 5 years after the question was asked, it finally got marked as the correct answer.Streaky
F
10

The setTitleShadowOffset for UIButton is deprecated. Use the shadowOffset of titleLabel property of UIButton

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

Fireboat answered 3/3, 2011 at 9:40 Comment(0)
M
8

In 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

Measurement answered 21/9, 2016 at 18:39 Comment(2)
This does not set a shadow on the actual text.Seavir
I must be mistaken. Sorry about that!Seavir
A
1

for Swift 3:

  button.setTitleShadowColor(UIColor.red, for: .normal)
  button.titleLabel?.shadowOffset = CGSize(width: 2, height: 2)
Amphoteric answered 22/10, 2016 at 12:41 Comment(0)
C
0

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

#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;
Croup answered 1/8, 2018 at 12:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.