Rounded corner UITextField Xcode

I have two text boxes (username and password) and want to make an upper rounded corner for the username and a lower rounded corner for the password according to the attachment.

enter image description here

+4


source to share


5 answers


Just create UIView

.

Place two text boxes in UIView

. Delete the border style UITextField

.



yourView.layer.cornerRadius = 10.0
yourView.clipsToBounds = true

      

+12


source


Just follow these steps

STEP 1: Importing the QuartzCore structure into your class:

      #import <QuartzCore/QuartzCore.h>

      



STEP 2: apply encoding

      textField.layer.cornerRadius=6.0f;
      textField.layer.masksToBounds=YES;
      textField.layer.borderColor=[[UIColor blueColor]CGColor];
      textField.layer.borderWidth= 1.0f; 

         or

      [textField.layer setBorderColor:[[UIColor blueColor] CGColor]];
      [textField.layer setBorderWidth:2.o];
      [textField.layer setCornerRadius:5.0]; 

      

+1


source


You can use:

(UIBezierPath *)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii

      

Example:

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:textField.bounds byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(10.0, 10.0)];

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.view.bounds;
maskLayer.path  = maskPath.CGPath;
textField.layer.mask = maskLayer;

      

0


source


I think you should write a CALayer extension. Swift 3.x - 4.x

extension CALayer {

    func addRadius(_ corners: UIRectCorner, radius: CGFloat, view: UIView) {
        let mask = CAShapeLayer()
        mask.bounds = view.frame
        mask.position = view.center
        mask.path = UIBezierPath(roundedRect: view.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)).cgPath
        view.layer.mask = mask
    }

    func addRadius(radius: CGFloat) {
        self.cornerRadius = radius
    }  
}

      

Using

    class ViewController: UIViewController {

        @IBOutlet var messageTextField: UITextField!

        override func viewDidLoad() {
            super.viewDidLoad()
            messageTextField.layer.addRadius(radius: 8.0)
        }
    }

      

I hope for your help :)

0


source


Here's a complete sample code:

UIKit import

class RoundedTextField: UITextField {

    override func awakeFromNib() {
        self.layer.cornerRadius = 10.0 //self.frame.size.height/2
        self.clipsToBounds = false

        super.awakeFromNib()
    }
}

      

0


source







All Articles