I have 3 UITextFields with border style none. I want to add borders in code. The effect I want to achieve is to have rounded top corners on first UITextField and to have rounded bottom corners on third text field. Code I am using for rounding edges is here Round top corners of a UIView and add border
But i get this - no right edge and corners are not rounded:
Note: I've set all constraints, that is not a problem. If i use UITextBorderStyleLine right edge is not rounded again.
Please help.
if you want to simplest way to do like on a screen look here>>>
Grey view with clip subviews mode on, and 3 labels/textfields inside, and 2 black view with 1 pixel height
in code..
self.viewCorner.layer.cornerRadius = 6;
self.viewCorner.layer.borderWidth = 1;
self.viewCorner.layer.borderColor = [UIColor blackColor].CGColor;
After you set constraints to grey view and 2 views with 1 pixel height like this
Grey view
1 pixel height view
and result on IPad simulator
Thats all, you can do this for 5 minutes
You need to create custom UItextField or method to change the top and bottom corner to oval shape. Here is a below sample code to top corner similarly you need to do it for bottom left and right corner.
CGRect rect = myTextField.bounds;
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect
byRoundingCorners:UIRectCornerTopLeft |UIRectCornerTopRight
cornerRadii:CGSizeMake(6.0, 6.0)];
CAShapeLayer *layers = [CAShapeLayer layer];
layers.frame = rect;
layers.path = path.CGPath;
myTextField.layer.mask = layers;
Related
I say true because while one can do tableView.layer.cornerRadius = 5.0, that method isn't 100%.
It works great when you have enough cells to fill the entire frame of the UITableView, but... if you don't have enough cells to fill it entirely, then you're in a rut because the cells won't be rounded.
I assume the best solution requires rounding the corners of the 1st cell (indexPath.row == 0) and the last cell (indexPath.row == data.count - 1)... but the problematic piece of this is that I need only the top 2 corners of the top cell rounded and the bottom two corners of the bottom cell rounded.
I've thought about using the UIBezierPath/CALayers method, but I don't know how costly it is and further, my cells all have custom heights, so I don't think I can accurately give my cell bounds that the UIBezierPath/CALayers method requires.
How would I achieve this?
The best way to achieve your need is to apply rounded corner to top-left and top-right to first cell and bottom-left and bottom-right to last cell.
Using following code you can apply rounded corner to specific corner of any view.
- (void)applyRoundCornersToView:(UIView *)view withCorners:(UIRectCorner)corners withRadius:(CGFloat)radius {
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = view.bounds;
maskLayer.path = maskPath.CGPath;
view.layer.mask = maskLayer;
}
You can use above function to apply rounded corner to desired corners.
To apply top corners rounded
[self applyRoundCornersToView:cell withCorners:UIRectCornerTopRight|UIRectCornerTopLeft withRadius:5.0];
To apply bottom corners rounded
[self applyRoundCornersToView:cell withCorners:UIRectCornerBottomRight|UIRectCornerBottomLeft withRadius:5.0];
So normally when you have a view and you want to add a drop shadow I do something like this:
UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:diptic.bounds];
diptic.layer.masksToBounds = NO;
diptic.layer.shadowColor = [UIColor blackColor].CGColor;
diptic.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
diptic.layer.shadowRadius = 10;
diptic.layer.shadowOpacity = .5f;
diptic.layer.shadowPath = shadowPath.CGPath;
Where diptic is my UIScrollView.
The problem is that because diptic is a scroll view, that I have content in the scroll view that I don't want to be on the screen until they scroll to it, so I want to have masksToBounds set to YES but if I do that then I can't see my shadow..
you can see the description of the dartboard ("A basically new dartboard...") I want to be hidden until the user scrolls to it. Also, the rounded corners of the diptic isn't clipping the square corners on the top of the image.
Is there any way to be selective of what is masked and what isn't?
Put diptic into another view with the same bounds, that way you can mask diptic to bounds but apply the shadow to the containing view that wouldn't have its content masked to its bounds.
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