Reputation: 131
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var myText: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Upvotes: 5
Views: 9451
Reputation: 1488
For iOS 11 onwards use Masked Corners property.
There is now a maskedCorners property for the layer. This is a CACornerMask which has four possible values for each corner:
Create an extension for better use:
extension UIView {
func roundCorners(corners:CACornerMask, radius: CGFloat) {
self.layer.cornerRadius = radius
self.layer.maskedCorners = corners
}
}
Example:
class CustomCell: UICollectionViewCell {
override func layoutSubviews() {
//Round left and righ top corners
yourView.roundCorners(corners: [.layerMinXMinYCorner, .layerMaxXMinYCorner], radius: radius)
}
}
Upvotes: 11
Reputation: 5451
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: .topLeft, cornerRadii: CGSize(width: 10.0, height: 10.0))
let maskLayer = CAShapeLayer()
maskLayer.path = path.cgPath
myText.layer.mask = maskLayer
You can also use in your UILabel
class
override func draw(_ rect: CGRect) {
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: .topLeft, cornerRadii: CGSize(width: 10.0, height: 10.0))
let maskLayer = CAShapeLayer()
maskLayer.path = path.cgPath
self.layer.mask = maskLayer
}
Upvotes: 4
Reputation: 68
Try out this It is working for me.
var textPath = UIBezierPath(roundedRect: myText.bounds, byRoundingCorners:
(.topLeft), cornerRadii: CGSize(width: 10.0, height: 10.0))
var textLayer = CAShapeLayer()
textLayer.frame = myText.bounds
textLayer.path = textPath.cgPath
myText.layer.mask = maskLayer
Upvotes: 2
Reputation: 636
Try this code
Tested in xcode 8 and swift3
extension UIView {
func roundCorners(corners:UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
self.layer.mask = mask
}
}
and use like this
lable.roundCorners(corners: [.topLeft], radius: 10)
Upvotes: 10
Reputation: 2419
You need to add these lines for seeting corner radius:
let radius = 15.0
let layer = CAShapeLayer()
let shadowPath = UIBezierPath(roundedRect: myText.frame, byRoundingCorners: ([.topLeft]), cornerRadii: CGSize(width: radius, height: radius ))
layer.path = shadowPath.cgPath
myText.layer.mask = layer
Upvotes: 2