amin sadeghian
amin sadeghian

Reputation: 131

how to set cornerRadius for only top-left corner of a UILabel?

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var myText: UILabel!

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

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

Upvotes: 5

Views: 9451

Answers (5)

Ariel Antonio Fundora
Ariel Antonio Fundora

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:

  • 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)
   }    
}

Upvotes: 11

Harshal Valanda
Harshal Valanda

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

Hemanshu
Hemanshu

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

Vikash Kumar
Vikash Kumar

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

Gourav Joshi
Gourav Joshi

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

Related Questions