I am trying to create glow just around the sides of the rectangle. The rest of the rectangle should be filled clear color. I create a UIView with CGRect of certain size and then -
self.backgroundColor = [UIColor clearColor];
self.layer.borderWidth = 2.0f;
self.layer.borderColor = [UIColor colorWithRed:203/255.0 green:1.0 blue:252/255.0 alpha:1.0f].CGColor;
self.layer.cornerRadius = 5.0f;
int glowSpread = 2;
CGRect glowRect = CGRectMake(self.bounds.origin.x-glowSpread, self.bounds.origin.y-glowSpread, self.bounds.size.width+2*glowSpread, self.bounds.size.height+2*glowSpread);
self.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:glowRect cornerRadius:5.0].CGPath;
self.layer.shadowColor = [UIColor colorWithRed:203/255.0 green:1.0 blue:252/255.0 alpha:1.0f].CGColor;
self.layer.shadowOffset = CGSizeMake(0, 0);
self.layer.shadowOpacity = 0.5;
self.layer.shadowRadius = 2.0f;
With the above approach, I get the glow around the side but the whole CGRect gets filled with the shadow color, which I do not want. Any suggestion?
make a more complicated shadow path that isn't enclosed, or just add a sublayer of the color you want.
Related
I have the following code in my UITableViewCell:
[self.layer setBorderColor:[UIColor blackColor].CGColor];
[self.layer setShadowRadius:10.0];
[self.layer setCornerRadius:5.0];
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(5.0, 5.0)];
[self.layer setShadowPath:path.CGPath];
[self.layer setShouldRasterize:YES];
[self.layer setRasterizationScale:[UIScreen mainScreen].scale];
when I run instrument and set color offscreen - rendered yellow, this causes the cell to be yellow. When I remove the shouldRasterize it doesn't shade the cells to yellow. What are ways to improve this?
This is greatly hurting my scrolling performance. I am just trying to set rounded corners with some shadows in it.
I'm doing rounded corners like this:
self.layer.shadowColor = [UIColor grayColor].CGColor;
self.layer.shadowOffset = CGSizeMake(0.05, 0.05);
self.layer.shadowOpacity = 10;
self.layer.shadowRadius = 1.5;
self.layer.masksToBounds = NO;
self.layer.shouldRasterize = YES;
[self.layer setBorderColor: [[UIColor whiteColor] CGColor]];
[self.layer setBorderWidth: 5.0];
I want to apply two types of border on a UIImageView:
One is the border on the layer of the UIImageView.
Second is the border around the layer of the UIImageView.
How can I do this?
Try
#define kBorderWidth 3.0
#define kCornerRadius 8.0
CALayer *borderLayer = [CALayer layer];
CGRect borderFrame = CGRectMake(0, 0, (imageView.frame.size.width), (imageView.frame.size.height));
[borderLayer setBackgroundColor:[[UIColor clearColor] CGColor]];
[borderLayer setFrame:borderFrame];
[borderLayer setCornerRadius:kCornerRadius];
[borderLayer setBorderWidth:kBorderWidth];
[borderLayer setBorderColor:[[UIColor redColor] CGColor]];
[imageView.layer addSublayer:borderLayer];
And don't forget to import QuartzCore/QuartzCore.h
This example will draw a boarder on the layer, but change it's frame slightly to make the border around the layer.
Another way
You must import
#import <QuartzCore/QuartzCore.h>
Then add code for your UIImageView
imgView.clipsToBounds = YES;
imgView.layer.cornerRadius = 8.0;
imgView.layer.borderWidth = 2.0;
imgView.layer.borderColor = [UIColor greenColor].CGColor;
An other way is to add another layer that goes a bit outside of the UIImageView's layer like so :
CALayer * externalBorder = [CALayer layer];
externalBorder.frame = CGRectMake(-1, -1, myView.frame.size.width+2, myView.frame.size.height+2);
externalBorder.borderColor = [UIColor blackColor].CGColor;
externalBorder.borderWidth = 1.0;
[myView.layer addSublayer:externalBorder];
myView.layer.masksToBounds = NO;
Swift 5
Beware when using a UIImageView; masksToBounds = false means the image will spill over
let outsideBorder = CALayer()
outsideBorder.frame = CGRect(x: -1, y: -1, width: myView.frame.size.width+2, height: myView.frame.size.height+2)
outsideBorder.borderColor = UIColor.black.cgColor
outsideBorder.borderWidth = 1.0
myView.layer.addSublayer(outsideBorder)
myView.masksToBounds = false
This question already has answers here:
Swift - Cut hole in shadow layer
(3 answers)
Closed 3 months ago.
I have a UIView with a translucent fill and a drop shadow. Since the fill is translucent, I can see the shadow behind the fill.
- (id)init
{
self = [super init];
if (self) {
self.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.8];
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowOffset = CGSizeMake(0.0, 0.0);
self.layer.shadowOpacity = 0.5;
self.layer.shadowRadius = 2.0;
self.layer.cornerRadius = 3.0;
}
return self;
}
I do not like this behavior. I cannot see anything behind the view because the fill's opacity + the shadow's opacity > 100%. How do I make it like CSS where the shadow is only drawn outside of a box's border?
.someStyle {
background: white;
opacity: 0.8;
box-shadow: 0 0 1em rgba(0,0,0,0.5);
}
I was able to get this desired effect using something like the following:
...
someLayer.backgroundColor = [[UIColor greenColor] CGColor];
someLayer.shadowOpacity = 1.0;
someLayer.shadowOffset = CGSizeMake(10.0, 10.0);
someLayer.shadowColor = [[UIColor blackColor] CGColor];
someLayer.rasterizationScale = [[UIScreen mainScreen] scale];
someLayer.shouldRasterize = YES;
someLayer.opacity = 0.5;
[[self layer] addSublayer:someLayer];
something exactly like above?
I know how to produce a rounded corner:
imageView.layer.cornerRadius = 10;
imageView.layer.shouldRasterize = YES;
imageView.layer.masksToBounds = YES;
For the shadow, I have tried
imageView.layer.shadowOffset = CGSizeMake(1, 1);
imageView.layer.shadowRadius = 5;
imageView.layer.shadowOpacity = 0.4;
imageView.layer.shadowColor = [UIColor blackColor].CGColor;
imageView.layer.shouldRasterize = YES;
But imageView.layer.masksToBounds = YES; from rounded corner kills the shadow.
Another question is that how to produce a shadow exactly like shown in the image? I produced this image in photoshop, I used 120 degree as the direction of the light. But if I used the code above, and turn off maskToBounds, I can see the shadow and it is ugly.
Or can I produce a rounded corner+shadow image frame in photoshop and apply the frame to every image in my app? I think that will give better performance. shadowing and cornering the images on the fly will have terrible performance if all images are on a scroll.
Thanks
Try this :
CALayer *sublayer = [CALayer layer];
sublayer.backgroundColor = [UIColor blueColor].CGColor;
sublayer.shadowOffset = CGSizeMake(0, 3);
sublayer.shadowRadius = 5.0;
sublayer.shadowColor = [UIColor blackColor].CGColor;
sublayer.shadowOpacity = 0.8;
sublayer.frame = CGRectMake(30, 30, 128, 192);
sublayer.borderColor = [UIColor blackColor].CGColor;
sublayer.borderWidth = 2.0;
sublayer.cornerRadius = 10.0;
[self.view.layer addSublayer:sublayer];
CALayer *imageLayer = [CALayer layer];
imageLayer.frame = sublayer.bounds;
imageLayer.cornerRadius = 10.0;
imageLayer.contents = (id) [UIImage imageNamed:#"BattleMapSplashScreen.jpg"].CGImage;
imageLayer.masksToBounds = YES;
[sublayer addSublayer:imageLayer];
And take look at the original source
I would use two image views. A background png that has the shadow (can be re-used for every image) and the foreground png image which has rounded corners.
You probably want to use a different layer for shadowing, and keep your masksToBounds and rounding code. In this example imageView is the name the image view that you want to shadow and round:
CALayer *shadowLayer = [[[CALayer alloc] init] autorelease];
sublayer.frame = imageView.bounds;
sublayer.masksToBounds = NO;
sublayer.shadowOffset = CGSizeMake(1, 1);
sublayer.shadowRadius = 5;
sublayer.shadowOpacity = 0.4;
sublayer.shadowColor = [UIColor blackColor].CGColor;
sublayer.shouldRasterize = YES;
[imageView.layer insertSublayer:shadowLayer atIndex:0]; // Put it underneath the image
That should give you the shadow. Now to avoid the slow recalculating, I suggest creating a UIImage out of the image. See this link: Create UIImage from shadowed view while retaining alpha?.
Hope this helps!
Due to UIProgressHUD need to access private api,
so I hope to construct an UIView with round corner and white border.
I know to make the corner round is:
view.layer.cornerRadius = 5;
But how to make the uiview has round corner and white border at the same time?
Welcome any comment
Thanks
interdev
Using the same layer object:
view.layer.borderWidth = 1;
view.layer.borderColor = [[UIColor whiteColor] CGColor];
Sometimes corner radius with white border does not work properly so I use UIBezierPath and CAShapeLayer.
For make the corner radius
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:self.imageView.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.view.bounds;
maskLayer.path = maskPath.CGPath;
self.imageView.layer.mask = maskLayer;
For make the border white
CAShapeLayer* borderShape = [CAShapeLayer layer];
borderShape.frame = self.imageView.bounds;
borderShape.path = maskPath.CGPath;
borderShape.strokeColor = [UIColor whiteColor].CGColor;
borderShape.fillColor = nil;
borderShape.lineWidth = 3;
[self.imageView.layer addSublayer:borderShape];
It will work. Hope this help
There are border properties in the layer of the view as well: eg:
view.layer.borderWidth = 1;
view.layer.borderColor = [UIColor redColor].CGColor;
view.layer.cornerRadius = 5;
view.clipsToBounds = YES;
view.layer.borderWidth = 1;
view.layer.borderColor = [UIColor whiteColor].CGColor;
code to get rounded corners and border
#import <QuartzCore/QuartzCore.h>
view.layer.cornerRadius = 10;
view.layer.borderWidth = 1;
view.layer.borderColor = [[UIColor whiteColor] CGColor];
[view.layer setBorderWidth:2];
[view.layer setBorderColor:[[UIColor whiteColor]CGColor]];