CALayer内边框圆角半径

6
我有一个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;
}

以下是现在的截图:

请注意,外边角已按预期进行了圆角处理,但边框的内部角是尖锐的,而不是圆角的。这就是我要解决的问题。感谢您的帮助!


4
请展示您的代码和TextView的截图。 - Vinay Jain
2个回答

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

尝试增加您的半径值。


我已经导入了QuartzCore,并尝试更改角半径,它起作用了!谢谢! - Mason

3

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

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

输出 :

输出 :

重要提示:

确保 cornerRadius 大于 borderWidth。否则您将无法看到区别。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接