Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically adding a shadow to a UIButton label

Tags:

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!

like image 369
edhog Avatar asked Mar 03 '11 09:03

edhog


People also ask

How to add shadow in storyboard xcode?

Go to the Storyboard. Add a Button to the main view and give it a title of "Shadow Tutorial". Select the Resolve Auto Layout Issues button and select Reset to Suggested Constraints. The Storyboard should look like this.

How to add shadow in iOS?

Add a shadowTap to select an object, or select multiple objects. , then tap Style. Turn on Shadow, then tap a shadow style.


2 Answers

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);
like image 99
fsaint Avatar answered Oct 20 '22 10:10

fsaint


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];
like image 45
ArtOfWarfare Avatar answered Oct 20 '22 10:10

ArtOfWarfare