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
.
Solution 2
Create extension to set corner radius from storyboard
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
Related videos on Youtube
Comments
-
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,Help me out in solving
-
Nirav D about 7 yearsAlso set
layer.masksToBounds
property with boolean valuetrue
. -
Nirav D about 7 years@WillForte Check the edited answer you get idea about it.
-
Forte Zhu about 7 yearsbut I didn't set it for button and views, those work fine.
-
Nirav D about 7 years@WillForte Have you try once by setting
masksToBounds
property?