Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

swift3

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var myText: UILabel!

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

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
 }
}
like image 769
amin sadeghian Avatar asked Jul 10 '17 05:07

amin sadeghian


People also ask

How do you label corner radius?

"label. layer. masksToBounds = true" is an important code, if you apply corner radius to a label and if it dosen't work than adding this will definitely add the corner radius to a particular label.. So this should be the correct answer..

What is corner radius?

The corner radius is a measurement describing the curve on the corners of your labels. This is measured in millimetres and refers to the radius of the circle created if the curve was extended to create a full circle.


1 Answers

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)
   }    
}
like image 106
Ariel Antonio Fundora Avatar answered Oct 16 '22 01:10

Ariel Antonio Fundora