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

11
我尝试使用User Defined Runtime Attributes将cornerRadius添加到UILabel中,但效果不如预期,cornerRadius没有设置。我想知道我哪里出错了。我附上了屏幕截图,

enter image description here

请帮我解决这个问题。

抱歉,我上传了错误的图片,已更新。 - Forte Zhu
3个回答

33
这里需要写成layer.cornerRadius而不是cornerRadius,并且你还需要将layer.masksToBounds设置为true

在这里输入图片描述


同时将 layer.masksToBounds 属性设置为布尔值 true - Nirav D
@WillForte 请检查已编辑的答案,您会对此有所了解。 - Nirav D
但我没有为按钮和视图设置它,它们工作得很好。 - Forte Zhu
@WillForte 你有没有尝试过设置 masksToBounds 属性? - Nirav D

9
创建扩展以从Storyboard设置圆角半径

enter image description here

public extension UIView {

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

3
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];
}

//现在你可以从属性检查器中设置它

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