border in UIImageView - iphone

I am trying to have an UIImageView bordered like the following:
I have tried using:
[imageView.layer setBorderColor: [[UIColor whiteColor] CGColor]];
[imageView.layer setBorderWidth: 2.0];
but then you can't see that gray border on the outside. It has that gray shadow effect on the outside.
How do I do this?

Take a look at the shadow properties of CALayer.
[imageView.layer setShadowOffset:CGSizeMake(-1.0, -1.0)];
[imageView.layer setShadowOpacity:0.5];

This question about adding shadows to UIImageView might help

imageView.layer.shadowOpacity=0.6;
imageView.layer.shadowRadius = 0.0;
imageView.layer.shadowColor = [UIColor grayColor].CGColor;
imageView.layer.shadowOffset = CGSizeMake(-2.0, 1.0);

Related

optimizing rounded corners performance

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

CALayer border get fuzzy on rotation

I am using
UIColor* clear = [UIColor blackColor];
[self.layer setBorderColor:[color CGColor]];
to set the border for an image.It is adding border but when i tried rotating that image to 45 degree,90 degree so on ,that border starts getting fuzzy and its not fixed or straight anymore. How can image that border fix like a line always whatever i do..
UIColor* clear = [UIColor blackColor]; [self.layer setBorderColor:[color CGColor]];
looks like something weird ;\ what is self here?
try to
UIImageView *imgView = [[UIImageView alloc] setImage:[UIImage imageNamed:#"pic.png"]];
[self.view addSubview:imgView];
imgView.layer.borderWidth = 2.0f;
imgView.layer.borderColor = [[UIColor blackColor] CGColor];
And don't forget to include QuartzCore framework and #import <QuartzCore/QuartzCore.h>

Background Color adjustment to rounded corners view

I have a view that I did for myself and rounded its corners. And when I try to adjust the backgroundColor property I end up with the background color there as a square, disrespecting my rounded corners.
Is there a solution?
Thanks in advance.
Call [[yourView layer] setMasksToBounds:YES]; and it should clip the rounded corners.
I'm pretty sure you won't even need the setClipsToBounds.
More on the subject of layers:
http://www.cimgf.com/2010/01/28/fun-with-uibuttons-and-core-animation-layers/
Try this code once.
self.settingsView.frame=CGRectMake(18, 73, 284, 210);
[self.settingsView setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.1]];
[self.settingsView setClipsToBounds:YES];
//Enable maskstobound so that corner radius would work.
[self.settingsView.layer setMasksToBounds:YES];
//Set the corner radius
[self.settingsView.layer setCornerRadius:10.0];
//Set the border color
[self.settingsView.layer setBorderColor:[[UIColor whiteColor] CGColor]];
//Set the image border`enter code here`
[self.settingsView.layer setBorderWidth:2.0];

UITextField setting backgroundcolor hides shadow

So I'm customizing my UITextField.layer with a shadow:
[userNameField.layer setBorderColor: [[UIColor colorWithRed:180/255.0 green:180/255.0 blue:180/255.0 alpha:1.0] CGColor]];
[userNameField.layer setBorderWidth: 1.0];
[userNameField.layer setCornerRadius:6.0];
[userNameField.layer setShadowOpacity:0.7];
[userNameField.layer setShadowColor:[[UIColor colorWithRed:180/255.0 green:180/255.0 blue:180/255.0 alpha:1.0] CGColor]];
[userNameField.layer setShadowOffset:CGSizeMake(0.5, 0.5)];
This works like a charm but the background is transparent.
Now when I set the background color:
[userNameField.layer setBackgroundColor:[[UIColor whiteColor] CGColor]];
The shadow is overwritten (hidden by the background color).
Does anyone know how to set both the background color AND the shadow on a text field?
You just need to explicitly set the masksToBounds property of the UITextField layer to NO like so:
myTextField.layer.masksToBounds = NO;
try this
textField.borderStyle = UITextBorderStyleRoundedRect;
You can set margin:
myTextField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 7, 7)];
Try this

Opacity of layer's shadow

I add shadow to the UIView:
[blurView.layer setCornerRadius:kCornerRadius];
blurView.layer.masksToBounds = NO;
blurView.layer.shadowColor = [[UIColor redColor] CGColor];
blurView.layer.backgroundColor = blurView.layer.shadowColor;
blurView.layer.shadowPath = [UIBezierPath
bezierPathWithRoundedRect:CGRectMake(-2.0*kCornerRadius, -2.0*kCornerRadius, blurView.bounds.size.width+4.0*kCornerRadius, blurView.bounds.size.height+4.0*kCornerRadius)
cornerRadius:kCornerRadius].CGPath;
blurView.layer.cornerRadius = kCornerRadius;
blurView.layer.shadowOffset = CGSizeMake(0.0, 0.0);
blurView.layer.shadowRadius = kCornerRadius;
blurView.layer.shadowOpacity = kOpacity;
blurView.layer.opacity = 1.0;
This part of code gives me a this view, like a uiview is blured:
When I try change opacity of blurView.layer (for example to 0.5), I get unexpected view:
As you can see - I get a sharp edges of uiview.
I think, shadow opacity is a part of opacity of layer - it's a cause of this 'error'. Can anybody help me to fix this? May be can I merge layers before change opacity of blurView.layer or something like this?
UPD
With this fix:
blurView.layer.shadowColor = [[UIColor colorWithRed:1 green:0 blue:0 alpha:0.5] CGColor];
I get this:
UPD
Answer is using setShouldRasterize method:
Thanks everybody!
[blurView.layer setShouldRasterize:YES];
The answer is using this code line:
blurView.layer.shouldRasterize = YES;
Maybe you could try putting this in?
blurView.layer.shadowColor = [[UIColor colorWithRed:1 green:0 blue:0 alpha:0.5] CGColor];
What happens if you put this :
blurView.layer.shadowOpacity = 1.0;
blurView.layer.opacity = 0.5;
I think that you might be setting the shadow opacity to 0.5 and then the whole layer to 0.5, making the shadow twice as transparent?
(or I'm completely wrong!)