I try to find every method I found, but I could not do it. I just want to make a label with rounded corners, a shadow with a background pattern. Shadow only works if I don't want rounded corners. I canβt get both of them together!
Here is my code with shadow:
label.text = msg; label.textAlignment = UITextAlignmentCenter; label.frame = CGRectMake(20,10,280,40); label.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"msg_box_bg.png"]]; [label.layer setCornerRadius:10]; [label.layer setMasksToBounds:NO]; /* Shadow */ label.layer.shadowColor = [UIColor blackColor].CGColor; label.layer.shadowOpacity = 0.6; label.layer.shadowOffset = CGSizeMake(0,0); label.layer.shadowRadius = 3;
It gives me a shadow with no rounded corners. But if I use
[label.layer setMasksToBounds:YES];
This will give me rounded corners without a shadow. I advised using a shadow path, so the code with the shadow path looks like this:
label.text = msg; label.textAlignment = UITextAlignmentCenter; label.frame = CGRectMake(20,10,280,40); label.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"msg_box_bg.png"]]; [label.layer setCornerRadius:10]; [label.layer setMasksToBounds:YES]; /* Shadow */ label.layer.shadowColor = [UIColor blackColor].CGColor; label.layer.shadowOpacity = 0.6; label.layer.shadowOffset = CGSizeMake(0,0); label.layer.shadowRadius = 3; label.layer.shadowPath = [[UIBezierPath bezierPathWithRoundedRect:label.frame cornerRadius:10]CGPath]; label.layer.shouldRasterize = YES;
This code gives me rounded corners, but no shadow. Any suggestions?
Thanks!
ios uilabel calayer shadow
hsnm
source share