iOS Label Shadowing - ios

I have this code here,
label.layer.shadowColor = [UIColor orangeColor].CGColor;
label.layer.shadowOffset = CGSizeMake(0,1);
label.layer.shadowRadius = 3.0;
label.layer.shadowOpacity = 0.5;
label.layer.shadowPath = [[UIBezierPath bezierPathWithRoundedRect:label.frame cornerRadius:20]CGPath];
I am using orange for testing purposes. Why is this not appearing as a shadow? All I am seeing is this, http://img12.imageshack.us/img12/1568/screenshot20130312at415.png
I am trying to get the gray label on the inside to have a shadow around the edges... Any help?
-Henry

You need to use label.bounds, not label.frame, to create the path. Also, make sure label.clipsToBounds is NO.

Try setting shadowOffset -1
label.layer.shadowOpacity = 1.0;
label.layer.shadowRadius =2.0;
label.layer.shadowColor = [UIColor blackColor].CGColor;
label.layer.shadowOffset = CGSizeMake(0.01,-1.0);
Also set label.bounds instead of label.frame
label.layer.shadowPath = [[UIBezierPath bezierPathWithRoundedRect:label.bounds cornerRadius:20]CGPath];

Related

Why CGColorRef does not change the CALayer properties?

I wanted to change view properties using CALayer associated with it.
I have created one function:
-(void)setupViewLayer{
viewLayer1.backgroundColor = (__bridge CGColorRef _Nullable)([UIColor blueColor]);
viewLayer1.borderColor =(__bridge CGColorRef _Nullable)([UIColor redColor]);
viewLayer1.borderWidth = 100.0;
viewLayer1.shadowOpacity = 0.7;
viewLayer1.shadowRadius = 10.0;
}
viewLayer1 is view's layer whose properties I want to change.
But the view does not show the properties.
Is there anything else I need to change? Please help.
Use this code -
viewLayer1.backgroundColor = [UIColor blueColor].CGColor;
viewLayer1.borderColor = [UIColor redColor].CGColor;
viewLayer1.borderWidth = 100.0;
viewLayer1.shadowOpacity = 0.7;
viewLayer1.shadowRadius = 10.0;
Hope this helps!
Set Color by,
viewLayer1.backgroundColor = [UIColor blueColor].CGColor;
viewLayer1.borderColor = [UIColor redColor].CGColor;

iOS Button with round corners and a shadow

I have searched and tried the various solutions but without success. What I would like is a button background with rounded corners and a shadow. I can make one or the other happen but not both at the same time. Any help would be very welcome.
viewDepositButton_.layer.cornerRadius = 5.0;
CAGradientLayer *viewLayer = [CAGradientLayer layer];
[viewLayer setColors:aloColors];
[viewLayer setFrame:viewDepositButton_.bounds];
[viewDepositButton_.layer insertSublayer:viewLayer atIndex:0];
viewDepositButton_.clipsToBounds = YES;
viewDepositButton_.layer.shadowColor = [UIColor colorWithRed:0.46 green:0.46 blue:0.46 alpha:1.0].CGColor;
viewDepositButton_.layer.shadowOpacity = 0.8;
viewDepositButton_.layer.shadowRadius = 8;
viewDepositButton_.layer.shadowOffset = CGSizeMake(8.0f, 8.0f);
viewDepositButton_.clipsToBounds = YES; or viewDepositButton_.layer.masksToBounds = YES; will clip everything outside your layer - including the shadow.
However you have few options:
Put your button under a parent view, which will have the same corner radius and the shadow you want, but will have clipsToBounds = NO. This way you'll have your button as a subview of a view which has the shadow.
Use an image for your button, which has the rounded corners and set your buttons clipsToBounds to NO
Hope this helps
I was able to get your code to work by the following and it looks fine. Make sure you call your methods in proper sequence.
viewDepositButton_.layer.cornerRadius = 5.0;
viewDepositButton_.layer.borderWidth = 1.0;
viewDepositButton_.layer.shadowColor = [UIColor colorWithRed:0.46 green:0.46 blue:0.46 alpha:1.0].CGColor;
viewDepositButton_.layer.shadowRadius = 8;
viewDepositButton_.layer.shadowOpacity = 0.8;
viewDepositButton_.layer.shadowOffset = CGSizeMake(8.0f, 8.0f);
CAGradientLayer *viewLayer = [CAGradientLayer layer];
[viewLayer setColors:aloColors];
[viewLayer setFrame:viewDepositButton_.bounds];
[viewDepositButton_.layer insertSublayer:viewLayer atIndex:0];
[viewDepositButton_ viewLayer];}
Here is my code for setting up a rounded corner button with shadow.
button.layer.cornerRadius = 15;
button.layer.shadowRadius = 2.0f;
button.layer.shadowColor = [UIColor lightGrayColor].CGColor;
button.layer.shadowOffset = CGSizeMake(-1.0f, 3.0f);
button.layer.shadowOpacity = 0.8f;
button.layer.masksToBounds = NO;
Overall, the code is pretty straightforward. Please comment if you have any question, concerns or bugs.

Applying shadow on uibutton Makes the button text blury

Here is my code to add a shadow on my uibutton which is created using a .XIB file
UIBezierPath *shadowPathEndbtn = [UIBezierPath bezierPathWithRect:CGRectMake(0, self->_navigationView.frame.size.height - 70,[UIScreen mainScreen].bounds.size.width , 2)];
self->_EndButton.layer.masksToBounds = NO;
self->_EndButton.layer.shadowColor = [UIColor blackColor].CGColor;
self->_EndButton.layer.shadowOffset = CGSizeMake(0.0f, 5.0f);
self->_EndButton.layer.shadowOpacity = 0.7f;
self->_EndButton.layer.shouldRasterize = YES;
self->_EndButton.layer.shadowPath = shadowPathForEndbtn.CGPath;
I am able to set the shadow using this code but this code makes my button text look blury. I am unable to figure out the exact reason behind this. Any help is Appreciated!
self->_EndButton.layer.masksToBounds = false
self->_EndButton.layer.shadowColor = [UIColor blackColor].CGColor
self->_EndButton.layer.contentsScale = [[UIScreen mainScreen]scale];
self->_EndButton.layer.shadowOpacity = 0.7f;
self->_EndButton.layer.shadowRadius = 10.0;
self->_EndButton.layer.shadowOffset = CGSizeMake(0,0)
This will give shadow and also the test is not blurry
Set button background color
self.EndButton.BackgroundColor=[UIColor WhiteColor];

Remove shadow from ViewController

I show shadow using the following code.
UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(317, 0, 1, self.navigationController.view.frame.size.height)];
self.navigationController.view.superview.layer.masksToBounds = NO;
self.navigationController.view.layer.shadowColor = [UIColor blackColor].CGColor;
self.navigationController.view.layer.shadowOffset = CGSizeMake(3, 0); /*Change value of X n Y as per your need of shadow to appear to like right bottom or left bottom or so on*/
self.navigationController.view.layer.shadowOpacity = 0.5f;
self.navigationController.view.layer.shadowPath = shadowPath.CGPath;
I want to remove this shadow.I have tried some thing but not working? Any help is appreciated.
try by replacing balckColor to clearColor and Opacity to 0.0
self.navigationController.view.layer.shadowColor = [UIColor clearColor].CGColor;
self.navigationController.view.layer.shadowOpacity = 0.0f;
can you not just set opacity to 0.0.
self.navigationController.view.layer.shadowOpacity = 0.0;
Thanks!!

Shadow effects on ImageView in iOS

I am trying to provide a shadow effect to my Imageview just like in this image.
but the problem which I am facing it is that the shadow is actually visible from the bottom of the Imageview.
Here is the code which I have done to add the shadow. The color and all is still not matching with this one.
CAGradientLayer *shadow = [CAGradientLayer layer];
shadow.frame = CGRectMake(-100, 70, perspectiveView.frame.size.width,
perspectiveView.frame.size.height - 20);
shadow.startPoint = CGPointMake(1.0, 0.5);
shadow.endPoint = CGPointMake(0, 0.5);
shadow.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithWhite:0.0
alpha:0.4f] CGColor], (id)[[UIColor clearColor] CGColor], nil];
shadow.opacity = 1.0f;
[perspectiveView.layer addSublayer:shadow];
Please provide inputs. Also if the approach is wrong, feel free to guide me to any other approach.
Thanks in advance.
Also can anyone suggest how to provide a 3D border just like in the image which provides a slight width to the image view?
Perhaps, adding a shadow to the layer seems to work. You may want to try something like this:
// perspectiveView.transform = CGAffineTransformMakeRotation(-50.0f);
perspectiveView.layer.shadowOffset = CGSizeMake(10, 10);
perspectiveView.layer.shadowRadius = 5.0;
perspectiveView.layer.shadowOpacity = 0.6;
perspectiveView.layer.masksToBounds = NO;
You will need to playaround with these values to match your requirements. Hope this helps.
Here is my output:
Try this,
- (void)setShadow {
[perspectiveView.layer setShadowOffset:CGSizeMake(-5.0, 5.0)];
[perspectiveView.layer setShadowRadius:5.0];
[perspectiveView.layer setShadowOpacity:1.0];
}
you can use CALayer class , the layer can manage your visual aspects like background color, border, shadow and with it you can also manage the geometry of it content like size , transform etc .
visual effect(shadowing)
self.yourView.layer.shadowOffset = CGSizeMake(0, 3);
self.yourView.layer.shadowRadius = 5.0;
self.yourView.layer.shadowColor = [UIColor blackColor].CGColor;
self.yourView.layer.shadowOpacity = 1;
and you also used it for like self.yourView.layer.frame(Geometry)
more info https://developer.apple.com/library/ios/documentation/graphicsimaging/reference/CALayer_class/index.html
Below code work i have checked properly.
but Shikhar varshney can u check setShadow method call and perspectiveView is not nil.
- (void)setShadow {
perspectiveView.layer.shadowColor = [UIColor colorWithWhite:0.0
alpha:0.4f] CGColor;
perspectiveView.layer.shadowOffset = CGSizeMake(0, 1);
perspectiveView.layer.shadowOpacity = 1;
perspectiveView.layer.shadowRadius = 1.0;
perspectiveView.clipsToBounds = NO;
}

Resources