Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding two shadows to a UILabel

I have a UILabel that I want to add two shadows.

A black colored shadow, and a white colored one.

One has -1 y-offset, and the other has 1 y-offset.

- (void)layoutSubviews{
    [super layoutSubviews];

    self.sectionTitleLabel.layer.shadowColor = [[UIColor whiteColor] CGColor];
    self.sectionTitleLabel.layer.shadowRadius = 0.0f;
    self.sectionTitleLabel.layer.shadowOpacity = .2;
    self.sectionTitleLabel.layer.shadowOffset = CGSizeMake(0.f, -1.f);

    CALayer *blackShadow = [[CALayer alloc] initWithLayer:self.sectionTitleLabel.layer];

    blackShadow.shadowColor = [[UIColor blackColor] CGColor];
    blackShadow.shadowRadius = 0.0f;
    blackShadow.shadowOpacity = .4;
    blackShadow.shadowOffset = CGSizeMake(0.f, 1.f);
    [self.sectionTitleLabel.layer addSublayer:blackShadow];

    self.sectionTitleLabel.layer.masksToBounds = NO;
}

With this the white shadow appears, but the black one does not.

like image 945
Tiago Veloso Avatar asked Dec 02 '25 16:12

Tiago Veloso


1 Answers

I don't understand what do you mean by "two shadows to a UILabel" but I hope I can help. If on this picture you can see what you want, I will be happy :)

enter image description here

- (void)viewDidLoad
{
   [super viewDidLoad];

   NSMutableAttributedString* attString = [[NSMutableAttributedString alloc] initWithString:self.custLabel.text];
   NSRange range = NSMakeRange(0, [attString length]);

   [attString addAttribute:NSFontAttributeName value:self.custLabel.font range:range];
   [attString addAttribute:NSForegroundColorAttributeName value:self.custLabel.textColor range:range];

   NSShadow* shadow = [[NSShadow alloc] init];
   shadow.shadowColor = [UIColor whiteColor];
   shadow.shadowOffset = CGSizeMake(0.0f, 1.0f);
   [attString addAttribute:NSShadowAttributeName value:shadow range:range];

   self.custLabel.attributedText = attString;

   [self nextShadow];

}

 -(void)nextShadow
 {
     self.custLabel.layer.masksToBounds = NO;
     self.custLabel.layer.cornerRadius = 5; 
     self.custLabel.layer.shadowOffset = CGSizeMake(3, 0);
     self.custLabel.layer.shadowRadius = 5;
     self.custLabel.layer.shadowOpacity = 1.5;
 }

I'd try to use

 - (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range;

But you can't use 2 shadow Attributes, only one. And you can customize

 -(void)nextShadow

method to create good solution, for example

 -(void)nextShadow
 {
     self.custLabel.layer.masksToBounds = NO;
     self.custLabel.layer.cornerRadius = 1;
     self.custLabel.layer.shadowOffset = CGSizeMake(1, 0);
     self.custLabel.layer.shadowRadius = 1;
     self.custLabel.layer.shadowOpacity = 1.5;
 }

enter image description here

If you adjust values in -(void)nextShadow you can get what you want.

like image 182
Joe Hallenbeck Avatar answered Dec 04 '25 08:12

Joe Hallenbeck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!