简体   繁体   English

圆角到UITextField Xcode

[英]rounded corner to UITextField Xcode

I have two textfield (username & password) and want to make top rounded corner for username and bottom rounded corner for password as per attachment. 我有两个文本字段(用户名和密码),并希望为用户名制作顶部圆角,并根据附件制作密码的底部圆角。

在此输入图像描述

Just create a UIView . 只需创建一个UIView

Put your two text fields in the UIView . 将您的两个文本字段放在UIView Remove the border style of UITextField . 删除UITextField的边框样式。

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

Just follow the below steps 请按照以下步骤操作

STEP 1:Import QuartzCore framework in you class: 第1步:在您的类中导入QuartzCore框架:

      #import <QuartzCore/QuartzCore.h>

STEP 2:Apply the coding 第2步:应用编码

      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]; 

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;

I think you should be write CALayer extension. 我想你应该写CALayer扩展。 Swift 3.x - 4.x 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
    }  
}

Use of 用于

    class ViewController: UIViewController {

        @IBOutlet var messageTextField: UITextField!

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

Hope your help :) 希望你的帮助:)

Here is a full code sample: 这是一个完整的代码示例:

import UIKit 导入UIKit

class RoundedTextField: UITextField {

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

        super.awakeFromNib()
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM