ios object-c 自定义可视编辑圆角与边框

本文介绍了如何在iOSObjective-C中使用IB_DESIGNABLE属性自定义UILabel等控件的圆角和边框,提供BaseLabel类的代码示例,以及如何在运行时动态设置这些样式。
摘要由CSDN通过智能技术生成

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 被注释因此设置完成后,只有在运行时控件才显示圆角,想设置时显示的去掉注释即可

  • 13
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玉笛0114

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值