圆角到UITextField Xcode

时间:2015-06-09 07:04:26

标签: ios swift xcode user-interface user-experience

我有两个文本字段(用户名和密码),并希望为用户名制作顶部圆角,并根据附件制作密码的底部圆角。

enter image description here

5 个答案:

答案 0 :(得分:12)

只需创建一个UIView

将两个文本字段放在UIView中。 删除UITextField

的边框样式
yourView.layer.cornerRadius = 10.0
yourView.clipsToBounds = true

答案 1 :(得分:1)

请按照以下步骤

第1步:在您的类中导入QuartzCore框架:

      #import <QuartzCore/QuartzCore.h>

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

答案 2 :(得分:0)

您可以使用:

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

示例:

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;

答案 3 :(得分:0)

我认为您应该编写CALayer扩展名。 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
    }  
}

使用

    class ViewController: UIViewController {

        @IBOutlet var messageTextField: UITextField!

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

希望您的帮助:)

答案 4 :(得分:0)

这是完整的代码示例:

导入UIKit

class RoundedTextField: UITextField {

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

        super.awakeFromNib()
    }
}