Rounded Rectangle Issue - iphone

I have seen many Q's on this subject but none are exactly what I am trying to do. I have a view inside of a view and it's frame is a CGRectangle. I would like said rectangle to have rounded edges to look like a rounded rectangle button. If I could have some code examples on how to implement that it would be nice. Thank you in advance.

You first need to import QuartzCore framework into your project. I hope you know how to do that. Than you can use the following code:
CALayer *l = [yourView layer];
[l setMasksToBounds:YES];
[l setCornerRadius:10.0];
// You can even add a border
[l setBorderWidth:1.0];
[l setBorderColor:[[UIColor blackColor] CGColor]];
Hope it helps! ;)

You need to make sure you import <QuartzCore/QuartzCore.h> and add QuartzCore to the Existing Frameworks in order to gain access to the cornerRadius: method.
Then to set the corner radius you would use something like the following depending upon your implementation of the view
UIView *theView = [[UIView alloc] initWithFrame:CGRectMake(10,10,100,200)];
CALayer *theViewLayer = [theView layer];
[theViewLayer setCorderRadius:5.0];
//Other Methods you can use
[theViewLayer setBorderColor:[[UIColor colorWithWhite:1.0 alpha:0.3] CGColor]];
[theViewLayer setBorderWidth:2.0];
[theViewLayer setBackgroundColor:[[UIColor blackColor] CGColor]];

Related

Draw an Item like home screen icon of iPhone

I want to draw an Item that similar as item of iPhone. I have also draw with UIView like this
But the item I want like this :
How can I draw like round buttton?
USE THIS CODE
UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
[bt setFrame:CGRectMake(10, 10, 50, 50)];
[bt setImage:[UIImage imageNamed:#"abc.png" ] forState:UIControlStateNormal];
// Get the Layer of any view
CALayer * l = [bt layer];
[l setMasksToBounds:YES];
[l setCornerRadius:10.0];
// You can even add a border
[l setBorderWidth:4.0];
[l setBorderColor:[[UIColor blueColor] CGColor]];
[self.view addSubview:bt];
Set the corner Radius of your view and insert the label below it.
iPhone supports the cornerRadius property on the CALayer class. Every view has a CALayer instance that you can manipulate.
First of all #import <QuartzCore/QuartzCore.h> and link to the QuartzCore framework to get access to CALayer's headers and properties.Then,
yourView.layer.cornerRadius = 8;
You can make round edges by
yourview.layer.cornerRadius = 8.0; // or other value

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>

CALayer Border is appearing above subview (Z-order related, I think)

I have searched but could not find the reason for this behavior.
I have a UIButton whose image I am setting. Here is how the button should appear. Note that this is just a photoshop of the intended button design:
Essentially, it is a square custom UIButton with a white border and a little surrounding shadow. In the upper right corner, there is a "X" mark, that will be added programmatically as a subview.
Here is the screenshot of the button within the actual app. At this point, I have only added a shadow and the X mark as a subview:
How, when I try to add the white border, here is what it looks like:
It seems that the white border is appearing above the X mark sublayer. I don't know why.
Here is the code that I am using:
// selectedPhotoButton is the UIButton with UIImage set earlier
// At this point, I am adding in the shadow
[selectedPhotoButton layer] setShadowColor:[[UIColor lightGrayColor] CGColor]];
[[selectedPhotoButton layer] setShadowOffset: CGSizeMake(1.0f, 1.0f)];
[[selectedPhotoButton layer] setShadowRadius:0.5f];
[[selectedPhotoButton layer] setShadowOpacity:1.0f];
// Now add the white border
[[selectedPhotoButton layer] setBorderColor:[[UIColor whiteColor] CGColor]];
[[selectedPhotoButton layer] setBorderWidth:2.0];
// Now add the X mark subview
UIImage *deleteImage = [UIImage imageNamed:#"nocheck_photo.png"];
UIImageView *deleteMark = [[UIImageView alloc] initWithFrame:CGRectMake(53, -5, 27, 27)];
deleteMark.contentMode = UIViewContentModeScaleAspectFit;
[deleteMark setImage:deleteImage];
[selectedPhotoButton addSubview:deleteMark];
[deleteMark release];
I don't understand why the border is appearing above the deleteMark subview. Is there any way to get the intended effect?
Thank you!
From Apple's docs on CALayer:
The border is drawn inset from the receiver’s bounds by borderWidth. It is composited above the receiver’s contents and sublayers and includes the effects of the cornerRadius property.
In order to get the effect you want, I suggest you put the image into an own subview/sublayer and set that sublayer's borderWidth property.
You can set the layer's zPosition to -1. That worked for me.
I had similar problem (I wanted to prevent border line to be on top of my subviews)
CAShapeLayer * _border = [CAShapeLayer layer];
_border.strokeColor = [UIColor colorWithRed:119/255.0f green:119/255.0f blue:119/255.0f alpha:1.0f].CGColor;
_border.fillColor = nil;
[bgRoundView.layer addSublayer:_border];
_border.path = [UIBezierPath bezierPathWithRoundedRect:bgRoundView.bounds cornerRadius:20.f].CGPath;

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

border in UIImageView

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