1

This is textfield which I get right now

I use next code:

- (void)setMaskByRoundingCorners:(UIRectCorner)corners withCornerRadius:(float)radius
    {
        UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)];

        CAShapeLayer* shape = [[CAShapeLayer alloc] init];
        [shape setPath:rounded.CGPath];
        shape.frame = self.bounds;
        self.layer.mask = shape;
    }

But now I see this strange effect.

I call it from viewcontroller, after didlayoutsubviews. I do that for main thread updating.

- (void)viewDidLayoutSubviews
{
    [self initUIfeatures];
}

- (void)initUIfeatures
{
    [authTextField setMaskByRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight|UIRectCornerBottomLeft) withCornerRadius:8.0f]; 
}

The issue is rounded corners getting cutoff.

Mohammad Zaid Pathan
  • 16,304
  • 7
  • 99
  • 130
Viktorianec
  • 361
  • 6
  • 22

2 Answers2

1

Add these functions,

-(void)roundCorners:(UIRectCorner)corners radius:(CGFloat)radius
{
    CGRect bounds = _IBtxtField.bounds;
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds
                                                   byRoundingCorners:corners
                                                         cornerRadii:CGSizeMake(radius, radius)];

    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = bounds;
    maskLayer.path = maskPath.CGPath;

    _IBtxtField.layer.mask = maskLayer;

    CAShapeLayer*   frameLayer = [CAShapeLayer layer];
    frameLayer.frame = bounds;
    frameLayer.path = maskPath.CGPath;
    frameLayer.strokeColor = [UIColor blackColor].CGColor;
    frameLayer.fillColor = nil;

    [_IBtxtField.layer addSublayer:frameLayer];
}

-(void)roundCornersRadius:(CGFloat)radius
{
    [self roundCorners:(UIRectCornerTopLeft|UIRectCornerTopRight | UIRectCornerBottomLeft) radius:radius];
}

Use like this,

[self roundCornersRadius:10];

Ref

SourceCode

Community
  • 1
  • 1
Mohammad Zaid Pathan
  • 16,304
  • 7
  • 99
  • 130
0

I have made custom class for the same. You can use below code any can change in storyboard as you want and the corner radius you want.

import UIKit

@IBDesignable
class CustomRoundedTextField: UITextField {

    @IBInspectable var lColor: UIColor = UIColor(red: (37.0/255.0), green: (252.0/255), blue: (244.0/255.0), alpha: 1.0)
    @IBInspectable var lWidth: CGFloat = 1
    @IBInspectable var lCornerRadius: CGFloat = 8
    @IBInspectable var sColor:UIColor = UIColor(red: (37.0/255.0), green: (252.0/255), blue: (244.0/255.0), alpha: 1.0)
    @IBInspectable var TLRCorner:Bool = false
    @IBInspectable var TRRCorner:Bool = false
    @IBInspectable var BLRCorner:Bool = false
    @IBInspectable var BRRCorner:Bool = false
    @IBInspectable var XInset:CGFloat = 10
    @IBInspectable var YInset:CGFloat = 10

    required internal init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    override internal func drawRect(rect: CGRect) {
        addBorderFieldRect()
    }

    override func textRectForBounds(bounds: CGRect) -> CGRect {
        return CGRectInset(bounds, XInset, 0)
    }

    override func editingRectForBounds(bounds: CGRect) -> CGRect {
        return CGRectInset(bounds, YInset, 0)
    }

    func addBorderFieldRect() {
        let rectanglePath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: [
            TLRCorner ? .TopLeft : [],
            TRRCorner ? .TopRight : [],
            BLRCorner ? .BottomLeft : [],
            BRRCorner ? .BottomRight : []
            ], cornerRadii: CGSizeMake(lCornerRadius, lCornerRadius))
        rectanglePath.closePath()
        self.lColor.setFill()
        rectanglePath.fill()
        self.sColor.setStroke()
        rectanglePath.lineWidth = lWidth
        rectanglePath.stroke()
    }

}
Parth Adroja
  • 13,198
  • 5
  • 37
  • 71