ios object-c 自定义可视编辑圆角与边框
1、常用控件:
UILabel、UIButton、UITextView、UIImageView、UITextField、UIView
2、示例代码:
BaseLabel.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
//IB_DESIGNABLE
@interface BaseLabel : UILabel
@property (nonatomic, assign) IBInspectable CGFloat cornerRadius; /**< 圆角半径 */
@property (nonatomic, strong) IBInspectable UIColor* borderColor; /**< 边框颜色 */
@property (nonatomic, assign) IBInspectable CGFloat borderWidth; /**< 边框宽度 */
@end
BaseLabel.m
#import "BaseLabel.h"
@implementation BaseLabel
//
- (void)setCornerRadius:(CGFloat)cornerRadius
{
_cornerRadius = cornerRadius;
self.layer.cornerRadius = cornerRadius;
self.layer.masksToBounds = cornerRadius > 0 ? YES : NO;
self.layer.shouldRasterize = YES;
self.layer.rasterizationScale = [UIScreen mainScreen].scale;
}
- (void)setBorderColor:(UIColor *)borderColor
{
_borderColor = [borderColor copy];
self.layer.borderColor = borderColor.CGColor;
}
- (void)setBorderWidth:(CGFloat)borderWidth
{
_borderWidth = borderWidth;
self.layer.borderWidth = borderWidth;
}
@end
3、使用
因IB_DESIGNABLE 被注释因此设置完成后,只有在运行时控件才显示圆角,想设置时显示的去掉注释即可