Reputation: 29326
I'm trying to give a label in one of the classes in my app a drop shadow, but it's not working at all. Nothing shows up. What am I doing wrong?
// Set label properties
titleLabel.font = [UIFont boldSystemFontOfSize:TITLE_FONT_SIZE];
titleLabel.adjustsFontSizeToFitWidth = NO;
titleLabel.opaque = YES;
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = titleLabelColor;
titleLabel.shadowColor = [UIColor blackColor];
titleLabel.shadowOffset = CGSizeMake(10, 10);
It's just white, no shadow.
Upvotes: 7
Views: 13790
Reputation: 12908
i hope you are aware of categories?
Creating a category will be better option:
Command + N > Objective-C Category > Category = Animation & Category on = UIView
This will create 2 files with name UIView+Animation.h
and UIView+Animation.m
UIView+Animation.h
file
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface UIView (Animation)
- (void)setBackgroundShadow:(UIColor *)shadowColor CGSize:(CGSize)CGSize shadowOpacity:(float)shadowOpacity shadowRadius:(float)shadowRadius;
@end
UIView+Animation.m
file
#import "UIView+Animation.h"
@implementation UIView (Animation)
- (void)setBackgroundShadow:(UIColor *)shadowColor CGSize:(CGSize)CGSize shadowOpacity:(float)shadowOpacity shadowRadius:(float)shadowRadius
{
self.layer.shadowColor = shadowColor.CGColor;
self.layer.shadowOffset = CGSize;
self.layer.shadowOpacity = shadowOpacity;
self.layer.shadowRadius = shadowRadius;
self.clipsToBounds = NO;
}
Import UIView+Animation.h
in any of your viewController
and call it like this:
[self.titleLabel setBackgroundShadow:[UIColor grayColor] CGSize:CGSizeMake(0, 5) shadowOpacity:1 shadowRadius:5.0];
Upvotes: 3
Reputation: 1991
Just Add this line to before adding titleLabel to self.view
titleLabel.layer.masksToBounds = NO;
Good Luck !!
Upvotes: 13
Reputation: 127
Just make sure that you allocate the UILabel and also set a frame for the label. And also make sure that view is added to the subview. Something like this:
titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];
titleLabel.font = [UIFont boldSystemFontOfSize:14];
titleLabel.adjustsFontSizeToFitWidth = NO;
titleLabel.opaque = YES;
titleLabel.text = @"My Label";
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.shadowColor = [UIColor blackColor];
titleLabel.shadowOffset = CGSizeMake(5, 5);
[myView addSubview:titleLabel];
[titleLabel release];
value 10 for shadow offset is quite big. You can tweak the values based on your requirements.
Upvotes: 0