5
import UIKit

class ViewController: UIViewController {

@IBOutlet weak var myText: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
 }
}
iAnurag
  • 9,286
  • 3
  • 31
  • 48
amin sadeghian
  • 131
  • 1
  • 10
  • Possible duplicate of [How to give cornerRadius for UIBezierPath](https://stackoverflow.com/questions/35053805/how-to-give-cornerradius-for-uibezierpath) – Harshal Valanda Jul 10 '17 at 05:52

5 Answers5

11

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:

  • layerMaxXMaxYCorner - bottom right corner
  • layerMaxXMinYCorner - top right corner
  • layerMinXMaxYCorner - bottom left corner
  • layerMinXMinYCorner - top left 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)
   }    
}
Ariel Antonio Fundora
  • 1,330
  • 15
  • 20
10

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)
Vikash Kumar
  • 642
  • 1
  • 11
  • 25
4
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
}
Harshal Valanda
  • 5,331
  • 26
  • 63
2

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
Gourav Joshi
  • 2,419
  • 2
  • 27
  • 45
2

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
Hemanshu
  • 68
  • 1
  • 7