iOS 视图样式 该怎么样复用

本文介绍了一种在iOS开发中使用闭包来管理和共享视图样式的方案,以减少重复代码并提高样式的一致性和可维护性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

其实也希望能像css那样处理iOS程序这种代码写的太多了 没有多少意思 思考一段时间了 觉得应该有办法的 我的办法是使用闭包

UILabel * label = [[UILabel alloc] initWithFrame:CGRectZero addToView:self.view];
label.backgroundColor = [UIColorredColor];
label.font = [UIFontsystemFontOfSize:16];
label.text =@"dfadfadfadfadfjka;fka;dfja";
label.layer.borderColor = [UIColoryellowColor].CGColor;
label.layer.borderWidth =1;
[label mas_makeConstraints:^(MASConstraintMaker *make) {
    make.center.mas_equalTo(self.view);
}];

看起来效果不错 但是目前还正在考虑继承系统的样式处理 有注意的伙伴不吝赐教

UIView+FunctionExtension.h

@interface UIView (StyleFormat)

/// 共享样式
@property (nonatomic, copy) void (^ _Nullable shareStyle) (id_Nonnull sourceView);

/// 独有样式
@property (nonatomic, copy) void (^ _Nullable uniqueStyle) (id_Nonnull sourceView);

- (void)shareStyle:(void(^_Nullable)(id_Nonnull sourceView))share uniqueStyle:(void(^_Nullable)(id_Nonnull sourceView))unique;

@end

 

UIView+FunctionExtension.m

@implementation UIView (StyleFormat)

- (void)setShareStyle:(void (^_Nullable)(id_Nonnull sourceView))shareStyle {
    if (shareStyle != nil) {
        shareStyle(self);
    }
    objc_setAssociatedObject(self,@selector(shareStyle), shareStyle,OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void (^ _Nullable)(id_Nonnull sourceView))shareStyle {
    return objc_getAssociatedObject(self,_cmd);
}

- (void)setUniqueStyle:(void (^_Nullable)(id_Nonnull sourceView))uniqueStyle {
    if (uniqueStyle != nil) {
        uniqueStyle(self);
    }
    objc_setAssociatedObject(self,@selector(uniqueStyle), uniqueStyle,OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void (^ _Nullable)(id_Nonnull sourceView))uniqueStyle {
    return objc_getAssociatedObject(self,_cmd);
}

- (void)shareStyle:(void(^_Nullable)(id_Nonnull sourceView))share uniqueStyle:(void(^_Nullable)(id_Nonnull sourceView))unique {
    self.shareStyle  = share;
    self.uniqueStyle = unique;
}

- (id)forwardingTargetForSelector:(SEL)aSelector {
    return [MessageObject new];
}

@end

MessageObject.h

@interface MessageObject : NSObject

@end

MessageObject.m

void realizationFunction(id obj,SEL_cmd);

@implementation MessageObject

+ (BOOL)resolveInstanceMethod:(SEL)sel {
    if (![self respondsToSelector:sel]) {
        class_addMethod([self class], sel, (IMP)realizationFunction,"v@:");
    }
    return YES;
}

@end

void realizationFunction(id obj,SEL_cmd) {
    NSLog(@"%@:%@", obj,NSStringFromSelector(_cmd));
}

目前除了打印有些不一样 是否有BUG进一步验证

2017-12-13 23:09:42.894667+0800 Layout[66916:2018402] (null):encodeWithOSLogCoder:options:maxLength:
2017-12-13 23:09:42.894907+0800 Layout[66916:2018402] (null):encodeWithOSLogCoder:options:maxLength:
2017-12-13 23:09:42.895193+0800 Layout[66916:2018402] (null):encodeWithOSLogCoder:options:maxLength:

异常的日志我的项目用到现在没有什么问题

工具链接 https://github.com/BetrayalPromise/MineCommonUtils 希望各位大佬赏赐一颗start

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值