扩展类添加属性,即运行时添加属性

在自定义扩展类时,可以通过运行时添加属性。

关键点在于,首先必须导入运行时头文件,即“#import <objc/runtime.h>”;其次,设置属性,即“objc_setAssociatedObject(self, &keyLineBottom, lineBottom, OBJC_ASSOCIATION_RETAIN);”;还有是获得设置的属性,如“UIImageView *lineBottom = objc_getAssociatedObject(self, &keyLineBottom);


示例如下(UITableViewCell添加底端分割线属性):

.h文件

#import <UIKit/UIKit.h>


@interface UITableViewCell (CellLine)


/// 设置分割线

- (void)showLineView:(CGFloat)offX color:(UIColor *)lineColor type:(CellLineShowType)type;


/// 底端分割线

@property (nonatomic, strong) UIImageView *lineViewButtom;


@end


.m文件

#import "UITableViewCell+CellLine.h"

#import <objc/runtime.h>


static NSString *const keyLineTop = @"lineTopView";

static NSString *const keyLineBottom = @"lineBottomView";


static CGFloat const heightLine = 0.5; // 默认分割线高度


@implementation UITableViewCell (CellLine)


/// 设置分割线

- (void)showLineView:(CGFloat)offX color:(UIColor *)lineColor type:(CellLineShowType)type

{

    // 底端分割线

    UIImageView *lineBottom = objc_getAssociatedObject(self, &keyLineBottom);

    if (!lineBottom)

    {

        lineBottom = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, (CGRectGetHeight(self.bounds) - heightLine), CGRectGetWidth(self.bounds), heightLine)];

        [self.contentView addSubview:lineBottom];

        

        objc_setAssociatedObject(self, &keyLineBottom, lineBottom, OBJC_ASSOCIATION_RETAIN);

    }


    CGRect rectButtom = lineBottom.frame;

    rectButtom.origin.x = offX;

    lineBottom.frame = rectButtom;

    

    lineBottom.backgroundColor = lineColor;

    lineBottom.image = [UIImage imageWithColor:lineColor andSize:CGSizeMake(1.0, 1.0)];

    

    // 顶端分割线

    UIImageView *lineTop = objc_getAssociatedObject(self, &keyLineTop);

    if (!lineTop)

    {

        lineTop = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth(self.bounds), heightLine)];

        [self.contentView addSubview:lineTop];

        

        objc_setAssociatedObject(self, &keyLineTop, lineTop, OBJC_ASSOCIATION_RETAIN);

    }

    

    CGRect rectTop = lineTop.frame;

    rectTop.origin.x = offX;

    lineTop.frame = rectTop;

    

    lineTop.backgroundColor = lineColor;

    lineTop.image = [UIImage imageWithColor:lineColor andSize:CGSizeMake(1.0, 1.0)];

    

    switch (type)

    {

        case CellLineShowNone:

        {

            lineBottom.hidden = YES;

            lineTop.hidden = YES;

        }

            break;

        case CellLineShowAll:

        {

            lineBottom.hidden = NO;

            lineTop.hidden = NO;

        }

            break;

        case CellLineShowButtom:

        {

            lineBottom.hidden = NO;

            lineTop.hidden = YES;

        }

            break;

        case CellLineShowTop:

        {

            lineBottom.hidden = YES;

            lineTop.hidden = NO;

        }

            break;

            

        default:

            break;

    }

}


#pragma mark - setter/getter


- (void)setLineViewButtom:(UIImageView *)lineViewButtom

{

    objc_setAssociatedObject(self, &keyLineBottom, lineViewButtom, OBJC_ASSOCIATION_RETAIN);

}


- (UIImageView *)lineViewButtom

{

    UIImageView *line = objc_getAssociatedObject(self, &keyLineBottom);

    return line;

}


- (void)setLineViewTop:(UIImageView *)lineViewTop

{

    objc_setAssociatedObject(self, &keyLineTop, lineViewTop, OBJC_ASSOCIATION_RETAIN);

}


- (UIImageView *)lineViewTop

{

    UIImageView *line = objc_getAssociatedObject(self, &keyLineTop);

    return line;

}


@end



转载于:https://my.oschina.net/potato512/blog/652449

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值