cornerRadius for UILabel using User Defined Runtime Attributes not working

11,381

Solution 1

It is layer.cornerRadius not just cornerRadius also you need to set layer.masksToBounds to true.

enter image description here

Solution 2

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

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

//Now you can set if from the Attribute Inspector

Share:
11,381

Related videos on Youtube

Forte Zhu
Author by

Forte Zhu

In search of the Best.

Updated on June 18, 2022

Comments

  • Forte Zhu
    Forte Zhu almost 2 years

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

    enter image description here

    Help me out in solving

  • Nirav D
    Nirav D about 7 years
    Also set layer.masksToBounds property with boolean value true.
  • Nirav D
    Nirav D about 7 years
    @WillForte Check the edited answer you get idea about it.
  • Forte Zhu
    Forte Zhu about 7 years
    but I didn't set it for button and views, those work fine.
  • Nirav D
    Nirav D about 7 years
    @WillForte Have you try once by setting masksToBounds property?