How to set cornerRadius only for the upper left corner of UILabel?

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var myText: UILabel!

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

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
 }
}
+4
source share
4 answers
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 class UILabel

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
}
+4
source

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 it

lable.roundCorners(corners: [.topLeft], radius: 10)
+4
source

You need to add these lines for the radius of the corners:

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
+2
source

Try it. This works 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
+1
source

Source: https://habr.com/ru/post/1681085/


All Articles