Applying shadow on uibutton Makes the button text blury - ios

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

Related

Unable to create shadow on UIButton with white background

I have tried to assign shadow to UIButton with the help of following code :
self.addCardButton.layer.masksToBounds = NO;
self.addCardButton.layer.shadowOffset =CGSizeZero;
self.addCardButton.layer.shadowRadius = 5;
self.addCardButton.layer.shadowOpacity = 1.0;
self.addCardButton.layer.cornerRadius = 5.0;
self.addCardButton.layer.borderColor = [[UIColor lightGrayColor]CGColor];
self.addCardButton.layer.borderWidth = 0.5;
self.addCardButton.layer.shadowColor = [[UIColor lightGrayColor] CGColor];
In viewdidload().
Still getting zero results.... please help me in knowing possible reasons for this glitch.

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.

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

iOS: programmatically draw a rectangle on top of a button

I need to draw a rectangle on top of a UIButton. Assume that there's a UIButton called hideMe, and this button has 100x50 size. I want to place a 100x10 rectangle on top of this button every time a user receives a message. If the user receives five messages, then the button will be completely covered by the rectangles. How do I do this?
CALayer *topBorder = [CALayer layer];
topBorder.borderColor = [UIColor redColor].CGColor;
topBorder.backgroundColor = [UIColor blackColor].CGColor;
topBorder.borderWidth = 2;
topBorder.frame = CGRectMake(0, 15, <tempButton>.frame.size.width, <tempButton>.frame.size.height-30);
[<tempButton>.layer addSublayer:topBorder];
The above code helps you to draw the rectangle.You can increase the layer frames to cover full.
try this
#import <QuartzCore/QuartzCore.h>
here we are drawing our own border rect and sub viewing it to our button
-(UIView*)borderRectWithYvalue:(CGFloat)yValue
{
UIView * rect = [[UIView alloc] initWithFrame:CGRectMake(0,yValue, 100, 10)];
rect.backgroundColor = [UIColor lightGrayColor];
rect.layer.borderColor = (__bridge CGColorRef)([UIColor redColor]);
rect.layer.borderWidth = 2.0f;
return rect;
}
and call this method when and where you want to add border to your button with your custom y value
for ex
[yourButton addSubview:[self borderRectWithYvalue:10.0]];

iOS Label Shadowing

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

Resources