繁体   English   中英

CALayer内边界角半径

[英]CALayer inner border corner radius

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

这是现在的屏幕截图:

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

尝试设置这个

[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; 看到我可以得到完美的输出。

在此处输入图片说明

尝试增加半径值。

导入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

输出:

输出:

重要:

确保cornerRadius大于borderWidth 否则,您将看不到区别。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM