xcode 使用用户定义的运行时属性的 UILabel 的cornerRadius 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42152679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:46:41  来源:igfitidea点击:

cornerRadius for UILabel using User Defined Runtime Attributes not working

iosobjective-cxcodeuilabel

提问by Forte Zhu

I try to add cornerRadius to UILabel using User Defined Runtime AttributesBut it is not working as expected, cornerRadius is not setting and I wonder where I made mistake. I attached screenshot of it,

我尝试使用添加cornerRadius到UILabelUser Defined Runtime Attributes但它没有按预期工作,cornerRadius没有设置,我想知道我在哪里犯了错误。我附上了它的截图,

enter image description here

在此处输入图片说明

Help me out in solving

帮我解决

回答by Nirav D

It is layer.cornerRadiusnot just cornerRadiusalso you need to set layer.masksToBoundsto true.

layer.cornerRadius不只是cornerRadius你也需要设置layer.masksToBoundstrue

enter image description here

在此处输入图片说明

回答by Yogesh Makwana

Create extension to set corner radius from storyboard

创建扩展以从故事板设置角半径

enter image description here

在此处输入图片说明

public extension UIView {

    @IBInspectable public var cornerRadius: CGFloat {
        get { return layer.cornerRadius }
        set { layer.cornerRadius = newValue }
    }
}

回答by Ahmed Sahib

Create a category of UIView
In .h file
///Below interface
@property (nonatomic) IBInspectable UIColor *borderColor;
@property (nonatomic) IBInspectable CGFloat borderWidth;
@property (nonatomic) IBInspectable CGFloat cornerRadius;

In .m file
//below Implementation
@dynamic borderColor,borderWidth,cornerRadius;


-(void)setBorderColor:(UIColor *)borderColor{
    [self.layer setBorderColor:borderColor.CGColor];
}

-(void)setBorderWidth:(CGFloat)borderWidth{
    [self.layer setBorderWidth:borderWidth];
}

-(void)setCornerRadius:(CGFloat)cornerRadius{
    [self.layer setCornerRadius:cornerRadius];
}

//Now you can set if from the Attribute Inspector

//现在您可以从属性检查器中设置是否