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

时间:2017-02-10 06:15:55

标签: ios objective-c xcode uilabel

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

enter image description here

帮我解决问题

3 个答案:

答案 0 :(得分:18)

layer.cornerRadius不仅仅是cornerRadius,还需要将layer.masksToBounds设置为true

enter image description here

答案 1 :(得分:8)

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

enter image description here

public extension UIView {

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

答案 2 :(得分:2)

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

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