Create a rectangle with just two rounded corners in swift?
In Swift 2.3 you could do so by
let maskPath = UIBezierPath(roundedRect: anyView.bounds,
byRoundingCorners: [.BottomLeft, .BottomRight],
cornerRadii: CGSize(width: 10.0, height: 10.0))
let shape = CAShapeLayer()
shape.path = maskPath.CGPath
view.layer.mask = shape
In Objective-C you could use the UIBezierPath
class method
bezierPathWithRoundedRect:byRoundingCorners:cornerRadii:
example implementation-
// set the corner radius to the specified corners of the passed container
- (void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners
{
UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds
byRoundingCorners:corners
cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *shape = [[CAShapeLayer alloc] init];
[shape setPath:rounded.CGPath];
view.layer.mask = shape;
}
and call the above method as-
[self setMaskTo:anyView byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight];
How to round edges of UILabel with Swift
Assuming you have added a backgroundColor
to your label otherwise there would be no way to tell if it had edges, you can use QuartzCore to round the edges of a label.
import QuartzCore
yourLabel.layer.backgroundColor = UIColor.redColor().CGColor
yourLabel.layer.cornerRadius = 5
yourLabel.layer.masksToBounds = true
Rounded labels in Interface Builder...?
Quoting from another SO answer:
iPhone OS 3.0 and later supports the cornerRadius property on the CALayer class. Every view has a CALayer instance that you can manipulate. This means you can get rounded corners in one line now:
That post has a pretty detailed description of how to implement this behavior.
Rounded corners of UILabel swift
I think you should set maskToBounds for textLabel. try this:
textLabel?.layer.masksToBounds = true
How do I create a round cornered UILabel on the iPhone?
iOS 3.0 and later
iPhone OS 3.0 and later supports the cornerRadius
property on the CALayer
class. Every view has a CALayer
instance that you can manipulate. This means you can get rounded corners in one line:
view.layer.cornerRadius = 8;
You will need to #import <QuartzCore/QuartzCore.h>
and link to the QuartzCore framework to get access to CALayer's headers and properties.
Before iOS 3.0
One way to do it, which I used recently, is to create a UIView subclass which simply draws a rounded rectangle, and then make the UILabel or, in my case, UITextView, a subview inside of it. Specifically:
- Create a
UIView
subclass and name it something likeRoundRectView
. - In
RoundRectView
'sdrawRect:
method, draw a path around the bounds of the view using Core Graphics calls like CGContextAddLineToPoint() for the edges and and CGContextAddArcToPoint() for the rounded corners. - Create a
UILabel
instance and make it a subview of the RoundRectView. - Set the frame of the label to be a few pixels inset of the RoundRectView's bounds. (For example,
label.frame = CGRectInset(roundRectView.bounds, 8, 8);
)
You can place the RoundRectView on a view using Interface Builder if you create a generic UIView and then change its class using the inspector. You won't see the rectangle until you compile and run your app, but at least you'll be able to place the subview and connect it to outlets or actions if needed.
How can I make a button have a rounded border in Swift?
Use button.layer.cornerRadius
, button.layer.borderColor
and button.layer.borderWidth
.
Note that borderColor
requires a CGColor
, so you could say (Swift 3/4):
button.backgroundColor = .clear
button.layer.cornerRadius = 5
button.layer.borderWidth = 1
button.layer.borderColor = UIColor.black.cgColor
How do I round only the top two corners of a UILabel?
You can do this using CALayers and masks
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.path = maskPath.CGPath;
label.layer.mask = maskLayer;
where maskPath is a UIBezierPath set up using bezierPathWithRoundedRect:byRoundingCorners:cornerRadii
Related Topics
Please Specify a Platform for This Target in Your Podfile
Xcode6/Swift: Unrecognized Selector Sent to Instance
Auto Focus and Auto Exposure in Avfoundation on Custom Camera Layer
Simplest Way to Implement a "Read More" Button to Expand a Uitextview in iOS Swift 2
Supporting a Nsmanagedobject Fetchrequest() Class Method in iOS 9 and 10
Import Xctest into a Dynamic Framework
Different Cell in Tableview Swift 3
Why am I Getting an Error Regarding Bolts Framework and Facebooksdk When I'm Not Even Using Bolts
Wkwebview Blank After 'Successful' Https Nsurlrequest
How to Crop a 4*3 Image in Photolibrary or Camera in Swift
How to Convert an Int into Nsdata in Swift
Changing the Shape of a Uiview Border
Swift Uinavigation Bottom Line and Shadow Remove Without Navbar Color Change
Using Cloud Code with the Parse Server and Heroku