CALayer内边角半径

时间:2013-11-28 05:19:21

标签: ios objective-c uitextview calayer

我有一个UITextView,我在其中设置边框宽度,边框颜色和角半径属性,外部看起来很棒。但是,边框的内部没有像外部一样的圆角,看起来很有趣。有没有办法绕过边界的内角?

编辑: 这是我在initWithFrame:方法中使用的代码:

- (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;
}

以下是现在的截图:

请注意,外角按预期圆角,但边框的内角是尖头而不是圆角。这就是我想要解决的问题。谢谢你的帮助!

2 个答案:

答案 0 :(得分:10)

尝试设置此项,

[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

修改

在您的情况下,kTextFieldCornerRadius的值可能设置为低。

如果我设置kTextFieldCornerRadius = 7;,请看我能获得完美的输出。

enter image description here

尝试增加半径值。

答案 1 :(得分:1)

导入QuartzCore框架并添加以下代码行:

目标 - 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 - 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 :

重要提示:

确保cornerRadius大于borderWidth。否则你将无法看到差异。