Trying to give Uilabel a shadow, but it will not appear

I am trying to give a short shadow in one of the classes of my application, but it does not work at all. I can not see anything. 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); 

He is just white, without a shadow.

+7
source share
3 answers

Just add this line before adding titleLabel to self.view

  titleLabel.layer.masksToBounds = NO; 

Good luck !!

+13
source

Hope you know the categories?

Creating a category would be the best option:

Command + N > Objective-C Category > Category = Animation & Category on = UIView This will create 2 files named 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 into any of the viewController and name it as follows:

 [self.titleLabel setBackgroundShadow:[UIColor grayColor] CGSize:CGSizeMake(0, 5) shadowOpacity:1 shadowRadius:5.0]; 
+3
source

Just make sure you assign UILabel and also set the frame for the label. And also make sure that the view is added to the view. Something like that:

  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]; 

a value of 10 for the shadow offset is quite large. You can customize the values ​​to suit your requirements.

0
source

All Articles