如何使用Swift在ios中创建带有corner-radius的textField?

时间:2016-06-28 09:01:04

标签: ios swift

我想创建一个像下面这样的textField

Expected output

我怎么能用swift做到这一点?

2 个答案:

答案 0 :(得分:2)

let path = UIBezierPath(roundedRect: yourtextField.bounds, byRoundingCorners: [.TopRight, .TopLeft], cornerRadii : CGSizeMake(15.0, 15.0))

        let templayer = CAShapeLayer()
        templayer.path = path.CGPath
        yourtextField.layer.mask = maskLayer

这应该有用。

答案 1 :(得分:0)

使用它 斯威夫特:

let maskPath: UIBezierPath = UIBezierPath(roundedRect: self.textField.bounds, byRoundingCorners: ([.TopLeft, .TopRight]), cornerRadii: CGSizeMake(10.0, 10.0))
    let maskLayer: CAShapeLayer = CAShapeLayer()
    maskLayer.frame = self.textField.bounds
    maskLayer.path = maskPath.CGPath
    self.textField.layer.mask = maskLayer
    let borderLayer: CAShapeLayer = CAShapeLayer()
    borderLayer.frame = self.textField.bounds
    borderLayer.path = maskPath.CGPath
    borderLayer.lineWidth = 4.0
    borderLayer.strokeColor = UIColor.blackColor().CGColor
    borderLayer.fillColor = UIColor.clearColor().CGColor
    self.textField.layer.addSublayer(borderLayer)

的OBJ-C

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

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

CAShapeLayer *borderLayer = [[CAShapeLayer alloc] init];
borderLayer.frame = self.textField.bounds;
borderLayer.path  = maskPath.CGPath;
borderLayer.lineWidth   = 4.0f;
borderLayer.strokeColor = [UIColor blackColor].CGColor;
borderLayer.fillColor   = [UIColor clearColor].CGColor;