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!

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
edhog
  • 513
  • 2
  • 6
  • 25

6 Answers6

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);
fsaint
  • 8,759
  • 3
  • 36
  • 48
  • 4
    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.) – ArtOfWarfare Jan 28 '13 at 20:10
  • Thanks @ArtOfWarfare. Haven't tried this in iOS 6 and the behavior may have changed. I'll test and update the code. – fsaint Jan 29 '13 at 17:49
  • 1
    @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. – ArtOfWarfare Sep 08 '13 at 04:19
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];
ArtOfWarfare
  • 20,617
  • 19
  • 137
  • 193
  • Setting `myKey.titleLabel.shadowColor` is not working indeed! – Berik Jan 28 '13 at 16:35
  • 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. – ArtOfWarfare Jan 28 '13 at 20:08
  • This is the best solution so far, just wanted to point that the proper way of doing this is using CGSizeMake not CGSize – Alejandro Luengo Apr 06 '15 at 01:01
  • 2
    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. – ArtOfWarfare Dec 16 '15 at 15:13
10

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

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

visakh7
  • 26,380
  • 8
  • 55
  • 69
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

Raphael Souza
  • 541
  • 4
  • 11
1

for Swift 3:

  button.setTitleShadowColor(UIColor.red, for: .normal)
  button.titleLabel?.shadowOffset = CGSize(width: 2, height: 2)
Wilson
  • 9,006
  • 3
  • 42
  • 46
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;
Userich
  • 316
  • 4
  • 4