In my nib file I have chosen one custom button and I want to set its corner radius. I have linked in the QuartzCore framework and then write the following code to set its corner radius
[[btnPressureLink layer] setCornerRadius:15.0];
and when I add a background colour for the button the corner radius is set but when I put an image on the button the corner radius does not get set. What should I do to set the corner radius when an image is used on the button?
Try
btnPressureLink.layer.cornerRadius = 15.0;
[btnPressureLink.layer setMasksToBounds:YES];
You can also use this,
[[btnPressureLink layer]setCornerRadius:4.0f];
[[btnPressureLink layer]setMasksToBounds:YES];
Import Quartz core and try this one....
button.layer.borderWidth = 2.0;
button.layer.cornerRadius = 12;
[button.layer setMasksToBounds:YES];
swift 4
self.buttonDone.layer.masksToBounds = true
//round shape
self.buttonDone.layer.cornerRadius = buttonDone.bounds.height/2
//custom shape
self.buttonCancel.layer.cornerRadius = 12
Related
Is there a way to make my image appear like this (rounded corners and with a light border).
I'm thinking of creating a white mask that has the middle transparent to place on top, would that work?
You can accomplish this, but instead of editing your UIImage, just do this in your UIImageView.
First, add the QuartzCore header
#import <QuartzCore/QuartzCore.h>
And then, edit the properties below, as you wish.
imageView.layer.cornerRadius = 5.0;
imageView.layer.borderColor = [[UIColor grayColor] CGColor];
imageView.layer.borderWidth = 2.0;
imageView.layer.masksToBounds = YES;
You're going to want to look at the QuartzCore docs, specifically the cornerRadius, borderColor and borderWidth properties within CALayer.
Does any one know how to achieve the shadow effect with no gradient? Like the screenshot show below
Another concern is the sequence of subviews, i.e the view in front may hide the effect of the view in behind. How to overcome this?
For the first problem you can change the shadowRadius of the shadow, for example:
//You must include QuartzCore framework (#import <QuartzCore/QuartzCore.h>)
view.layer.cornerRadius = 5;
view.layer.shadowRadius = 0; //The shadow should be rendered as a solid shape
view.layer.shadowOffset = CGSizeMake(0, 2);
view.layer.shadowOpacity = 0.5;
view.layer.shadowColor = [UIColor blackColor].CGColor;
UIBezierPath *path = [UIBezierPath bezierPathWithRect:view.bounds];
view.layer.shadowPath = path.CGPath; //This is very important!
Remember to always set the shadowPath! If you don't the performance of rendering the shadow will decrease a lot.
For the second problem, sorry but I don't think there's a way to let the shadow of an object appear over another view that is over the original one.
I have a view with a border of 10 pixels drawn on the method.
I need to update the border color and I use [self setNeedsDisplay] to make it redraw
the view.
Since I need to update only the border I want to use : [self setNeedsDisplayInRect:rect] so it will draw only the border.
How can I get a rect of only the border with out the other areas of the view?
Thanks
Shani
You can't because a CGRect is rectangle, so it is a convex shape that can't have holes in it.
But you can decompose the border into four rectangles and call [self setNeedsDisplayInRect:rect] four times.
Also, if you import QuartzCore, you can probably use the property borderColor of the view's layer:
#import <QuartzCore/QuartzCore.h>
// ...
view.layer.borderWidth = 10;
view.layer.borderColor = [UIColor redColor].CGColor;
// And to change it later
view.layer.borderColor = [UIColor greenColor].CGColor;
You could get four CGRects around each part of the border (top, right, bottom, and left) and call the method four times with each of them.
I'm trying to draw a custom button frame as follows:
UIBezierPath *stroke = [UIBezierPath bezierPathWithRoundedRect:self.bounds
cornerRadius:RECT_CORNECR_RADIUS];
[stroke stroke];
But for some reason the corner curves look thinker than the sides. If you look at the UIButton's default frame it's very uniform. A is a UIButton, B is a custom button.
Any ideas how I can make it more like the UIButton.
You are stroking the bounds of your button. This will draw your line centred over the edge the view, so half of the thickness of the line is outside the bounds and is not drawn. This is why it is full thickness in the corners. Use CGRectInset on your bounds rectangle (inset by half the thickness of your line) and stroke THAT rect.
The problem you have is probably due to antialiasing. You can try to change the antialiasing settings of CoreGraphics before drawing your beizerPath.
An easier solution is to use the CALayer of your button and its cornerRadius property. It would be easier to draw a rounded corner
If self is your custom button:
self.layer.cornerRadius = RECT_CORNER_RADIUS;
self.layer.borderWidth = 1.0f;
self.layer.borderColor = [UIColor blackColor].CGColor;
Of course don't forget to import the QuartzCore framework and import its header for this to work (#import )
I want to add a corner radius to a UIButton. It is working fine but a problem occurs when I add image to it.
It does not round its corners with images, the image is shown in full rectangle form.
Please see the image, I have used the corner radius with red color and the output is as follow:
Please help.
Did you try to use set the masksToBounds: property? Fore example:
CALayer *layer = [myView layer];
[layer setMasksToBounds:YES];
[layer setCornerRadius:8.0];
That should do the trick.
you use -
myButton.imageView.layer.cornerRadius = 5;
but make sure that your image size is exact same as button size. its working for me.
yourButton.layer.cornerRadius = 10 //this value should be half of your button's height to make a circle
yourButton.clipsToBounds = true //this clips everything outside of bounds