问题
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
回答1:
It is layer.cornerRadius
not just cornerRadius
also you need to set layer.masksToBounds
to true
.
回答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 }
}
}
回答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
来源:https://stackoverflow.com/questions/42152679/cornerradius-for-uilabel-using-user-defined-runtime-attributes-not-working