How to make iOS 8 shadow darker? - ios

I have used an iOS 8 shadow property, and it's not dark enough. Here is my code:
View.layer.shadowOffset = CGSizeMake(1, 1);
View.layer.shadowColor = [[UIColor blackColor] CGColor];
View.layer.shadowRadius = 6.0f;
View.layer.shadowOpacity = 1.0f;
I need to make the shadow darker, so that it's almost black. And, is there a way to create a shadow or override the default property programmatically?

Related

How to get shadow for UIImage in iOS using objective C

As can be seen in the image above there is a black shadow at the bottom of the image. I want similar effect in table view.( I want shadow for every line or the seperator between the table view cells.) The shadow effect should be more on the center as can be seen above and it should fade at its extremes.. Kindly help..
You can add a shadow by adding it to the view's layer (change the values to get your desired effect):
view.layer.shadowColor = [UIColor blackColor].CGColor;
view.layer.shadowOffset = CGSizeMake(0.0f, 2.0f);
view.layer.shadowOpacity = 0.5f;
view.layer.shadowRadius = 4.0f;
As you mentioned, you want to use it inside a UITableView, then you should provide a shadowPath as well for the best performance:
- (void)layoutSubviews
{
[super layoutSubviews];
view.layer.shadowPath = [UIBezierPath bezierPathWithRect:view.bounds].CGPath;
}

How to set 3d effect to UIButton

Hi i am try to set 3d effect to UIButton but my code does n't set properly.
it's come like this
but my requirement is like below
could you please help me some one.
My code is below:
self.submitBtn1.backgroundColor = [self colorWithHexString:#"a53129"];
self.submitBtn1.layer.cornerRadius = 3.0;
self.submitBtn1.layer.borderWidth = 2.0;
self.submitBtn1.layer.borderColor = [[UIColor clearColor] CGColor];
Use the following code to achieve this, set button type to custom
self.submitBtn.backgroundColor = [UIColor colorWithRed:(200.0f/255.0f) green:0.0 blue:0.0 alpha:1.0];
self.submitBtn.layer.cornerRadius = 3.0;
self.submitBtn.layer.borderWidth = 2.0;
self.submitBtn.layer.borderColor = [[UIColor clearColor] CGColor];
self.submitBtn.layer.shadowColor = [UIColor colorWithRed:(100.0f/255.0f) green:0.0 blue:0.0 alpha:1.0].CGColor;
self.submitBtn.layer.shadowOpacity = 1.0f;
self.submitBtn.layer.shadowRadius = 1.0f;
self.submitBtn.layer.shadowOffset = CGSizeMake(0, 3);
I am attaching image, shows result of this code.
Try playing around with the layer's shadow parameters. Like so:
self.submitBtn1.layer.shadowRadius = 5
self.submitBtn1.layer.shadowOpacity = 0.8
self.submitBtn1.layer.shadowOffset = CGSize(width: 5, height: 5)
Oh, and of course...
submitBtn1.setTitle(submitBtn1.currentTitle?.uppercaseString, forState: .Normal)
;)
Shadow is what makes the button look like a 3D object. Use this code below to get shadow for 3D like effect. Change to desired values, as per your requirements.
submitBtn1.layer.shadowRadius = 5
submitBtn1.layer.shadowOpacity = 0.8
submitBtn1.layer.shadowOffset = CGSize(width: 5, height: 5)

text with sharp shadow around

I want to achieve the text appearance like in the pictures below:
Now I'm working on shadow around the letters and I can't figure out how to do that.
What I've tried so far:
- The solution with:
label.shadowColor = [UIColor blackColor];
label.shadowOffset = CGSizeMake(1, 2);
gives a nice sharp shadow, but it doesn't fit my needs by two reasons:
It gives a shadow only from one side, set up by shadowOffset, whereas I need a "wrapping" shadow;
This solution doesn't give the soft part of the shadow (gradient) as there is in the pictures;
-The solution with:
label.layer.shadowOffset = CGSizeMake(0, 0);
label.layer.shadowRadius = 5.0;
label.layer.shouldRasterize = YES;
label.layer.shadowOpacity = 1;
label.layer.shadowColor = [UIColor blackColor].CGColor;
label.layer.masksToBounds = NO;
Works great, but it gives too soft shadow even though the shadowOpacity is set to 1 and the shadowColor is set to black:
Obviously it's not enough and I already think about drawing in labels' context. But it is not clear to me how would I achieve the goal even through context drawing.
Any idea would be much appreciated.
Try this
Create a Custom UILabel SubClass and Override the following method
- (void)drawTextInRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextSetShadow(context, CGSizeMake(0.0, 0.0), 10);
CGContextSetShadowWithColor(context, CGSizeMake(0.0, 0.0), 10, [UIColor blackColor].CGColor);
[super drawTextInRect:rect];
CGContextRestoreGState(context);
}
and this bottomColorLayer to the Label
CALayer *bottomColorLayer = [CALayer layer];
bottomColorLayer.frame = CGRectMake(0, labelRect.size.height/2, labelRect.size.width, labelRect.size.height/2);
bottomColorLayer.backgroundColor = [[UIColor colorWithWhite:0 alpha:.5] CGColor];
[label.layer insertSublayer:bottomColorLayer above:(CALayer *)label.layer];
or If you want Gradient
CAGradientLayer *bottomGradient = [CAGradientLayer layer];
bottomGradient.frame = CGRectMake(0, labelRect.size.height/2, labelRect.size.width, labelRect.size.height/2);
bottomGradient.cornerRadius = 0.0f;
bottomGradient.colors = #[(id)[[UIColor colorWithWhite:1 alpha:.5] CGColor], (id)[[UIColor colorWithWhite:0 alpha:.5] CGColor]];
[label.layer insertSublayer:bottomGradient above:(CALayer *)label.layer];
Use an explicit shadow path that's the shape you want. It sounds like you want a shadow the same shape as your text, but larger, with each shadow-letter centered on its corresponding letter. Once you have that, use the shadow radius to soften the edges of the shadow to whatever degree you want.
The code you have relies on the shadow radius alone to make the shadow larger than the text, which removes your ability to control the softness.
Try with the code below :-
[_testLable setText:#"TION ERRO"];
[_testLable setTextColor:[UIColor whiteColor]];
[_testLable setFont:[UIFont boldSystemFontOfSize:22] ];
_testLable.layer.shadowOffset = CGSizeMake(0, 0);
_testLable.layer.shadowRadius = 10.0;
_testLable.layer.shouldRasterize = YES;
_testLable.layer.shadowOpacity = 1;
_testLable.layer.shadowColor = [UIColor blackColor].CGColor;
_testLable.layer.masksToBounds = NO;
CGPathRef shadowPath = CGPathCreateWithRect(_testLable.bounds, NULL);
_testLable.layer.shadowPath = shadowPath;
CGPathRelease(shadowPath);
Its output is like this:-

solid shadow for CALayer

I have an animated CALayer which is dropping a shadow. That is what I intended to do but I am looking for way to have a solid shadow without blur. Changing the blur radius does not save my problem.
Any ideas?
Many thanks.
myLayer.shadowRadius = 3;
//shadowBlurRadius
myLayer.shadowColor = [[UIColor greenColor] CGColor];
//intended is to get a green solid shadow without blur
If you want no blurring, you can:
myLayer.shadowColor = [[UIColor greenColor] CGColor];
myLayer.shadowOffset = CGSizeMake(5.0, 5.0);
myLayer.shadowOpacity = 1.0;
myLayer.shadowRadius = 0.0;
The shadowRadius dictates the amount of blur. shadowOffset dictates where the shadow goes.

UITableView with shadow and corner radius (movie example attached)

I have a UIViewController with a UITableView. The sits inside the view controller's view with some padding on all sides. I am trying to draw a drop shadow from the table view as well as rounded corners, but I am unable to achieve both at the same time. I have tried with the following code and turned masksToBounds on and off, but setting it to NO create a really weird effect with the shadow scrolling and the calls are not clipping inside the table.
[self.tableView.layer setShadowColor:[UIColor blackColor].CGColor];
[self.tableView.layer setShadowOffset:CGSizeMake(0, 3)];
[self.tableView.layer setShadowOpacity:0.4];
[self.tableView.layer setShadowRadius:3.0f];
[self.tableView.layer setCornerRadius:5.0f];
[self.tableView.layer setShadowPath:[[UIBezierPath bezierPathWithRoundedRect:self.tableView.bounds cornerRadius:5.0f] CGPath]];
I am just drawing a UITableView with plain style too. The effect I am trying to achieve can be seen in the free app Transit App and here's a movie where you can see the shadow stays and the table even has a mask that scrolls up and down.
I have searched and searched and haven't been able to put together a solution based on other SO answers or online articles. Help appreciated.
Try the below links, some of them are old but you can get some pointer.
Custom Table Effects
Dropshadow 2
Cool Table Views
DropShadow
Okay!! I attempted once again and got an effect, it is again a work around only (instead of using images), the below is what i tried
Create a CALayer, with the same frame as your tableview, give the same curve and all, add the table view on top of this layer, thats it.
CALayer *sublayer = [CALayer layer];
sublayer.backgroundColor = [UIColor blueColor].CGColor; // If you dont give this, shadow will not come, dont know why
sublayer.shadowOffset = CGSizeMake(0, 3);
sublayer.shadowRadius = 5.0;
sublayer.shadowColor = [UIColor blackColor].CGColor;
sublayer.shadowOpacity = 1.0;
sublayer.cornerRadius = 5.0;
sublayer.frame = CGRectMake(myTable.frame.origin.x, myTable.frame.origin.y, myTable.frame.size.width, myTable.frame.size.height);
[self.view.layer addSublayer:sublayer];
[self.view.layer addSublayer:myTable.layer];
Ah!! due to some reason I am unable to upload the screenshot from my machine, firewall ;-). I will try from my home.
-anoop
It is recommended to use images for drop shadows with large tableviews, all that drawing has the ability to slow down performance, which can result in bad user experience, just a word of advice
I know this is a rather old question, but I recently had the same problem and the solutions here were good examples, but not full enough. So here it is the method I'm using to create shadow of a table view. It can be used also for any other view.
-(void)makeShadowToView:(UIView*) view withHeight:(CGFloat)shadowHeight
{
CGRect bounds = view.frame;
bounds.size.height = shadowHeight;
view.clipsToBounds = NO; // Without this row it doesn't display any shadow, at least for a table view
view.layer.shadowColor = [UIColor blackColor].CGColor;
view.layer.shadowOpacity = 0.9f;
view.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
view.layer.shadowRadius = 5.0f;
view.layer.shadowPath = [UIBezierPath bezierPathWithRect:bounds].CGPath;
}
Example usage: [self makeShadowToView:self.view withHeight:height];
where height is added in case you want to recalculate the shadow's height according to the table content's height.
(void)shadowForView{
CALayer *layer = self.viewActionMenu.layer;
layer.shadowOffset = CGSizeMake(-2, 0);
layer.shadowColor = [[UIColor blackColor] CGColor];
layer.shadowRadius = 2.0f;
layer.shadowOpacity = 0.80f;
// [self.view bringSubviewToFront:self.actionView];
}
And Call:
[self shadowForView];
Try this
UITableView * objTableview=[[UITableView alloc]initWithFrame:CGRectMake(x, y, w, h)];
UIView * objViewShadow=[[UIView alloc]initWithFrame:objTableview.bounds];
objTableview.layer.cornerRadius=5;
// add shadow
objViewShadow.layer.shadowOffset = CGSizeMake(0, 3);
objViewShadow.layer.shadowRadius = 5.0;
objViewShadow.layer.shadowColor = [UIColor blackColor].CGColor;
objViewShadow.layer.shadowOpacity = 0.8;
[objViewShadow addSubview:objTableview];
[self.view addSubview:objViewShadow];
You need to set this two properties
self.tableView.clipsToBounds = NO;
self.tableView.layer.masksToBounds = NO;

Resources