Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CALayer inner border corner radius

I have a UITextView where I set the border width, border color, and corner radius properties on its layer, and the outside looks great. However, the inner part of the border doesn't have rounded corners like the outer part, and it looks kind of funny. Is there a way to round the inner corners of the border?

Edit: Here's the code I used in the initWithFrame: method:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColorFromRGB(0xdedede);
        self.layer.cornerRadius = kTextFieldCornerRadius;
        self.layer.borderColor = UIColorFromRGB(0xD4974C).CGColor;
        self.layer.borderWidth = 3.0f;
        self.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:12.0f];
        [self setClipsToBounds:YES];
        [self.layer setMasksToBounds:YES];
    }
    return self;
}

And here's a screenshot of what it looks like now:

Notice the outer corners are rounded as expected, but the inner corners of the border are pointed rather than rounded. That's what I'm trying to fix. Thanks for your help!

like image 349
Mason Avatar asked Nov 28 '13 05:11

Mason


2 Answers

Try to set this,

[txtView       setClipsToBounds:YES]; //Confirms subviews are confined to the bounds of the view
[txtView.layer setMasksToBounds:YES]; //Confirms sublayers are clipped to the layer’s bounds

EDIT

Probably the value of kTextFieldCornerRadius is set to low in your case.

If I set kTextFieldCornerRadius = 7; see I can get perfect output.

enter image description here

try to increase your radius value.

like image 101
βhargavḯ Avatar answered Oct 02 '22 02:10

βhargavḯ


Import QuartzCore framework and add the following lines of code :

OBJECTIVE - C

UIView *yourView=[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 430)];
yourView.layer.borderColor = [UIColor redColor].CGColor;
yourView.layer.borderWidth = 10.0f;
yourView.layer.cornerRadius = 20.0f;

[yourView setClipsToBounds:YES];
[yourView.layer setMasksToBounds:YES];

SWIFT - 3.0.1 (playground code)

//: Playground - noun: a place where people can play

 import UIKit
 import PlaygroundSupport
 import QuartzCore

let containerView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 375.0, height: 100.0))

containerView.backgroundColor = UIColor.white
containerView.layer.borderWidth = 10
containerView.layer.borderColor = UIColor.red.cgColor
containerView.clipsToBounds = true
containerView.layer.masksToBounds = true
containerView.layer.cornerRadius = 20

 PlaygroundPage.current.liveView = containerView
 PlaygroundPage.current.needsIndefiniteExecution = true

OUTPUT :

OUTPUT :

IMPORTANT:

Make sure the cornerRadius is greater than the borderWidth . Else you will not be able to see the difference.

like image 21
abhimuralidharan Avatar answered Oct 02 '22 02:10

abhimuralidharan