Storyboard和xib中添加属性(圆角,前景色...) IBInspectable和IBDesignable

4 篇文章 0 订阅

缘起

iOS开发中,工程师会接触到大量的视图呈现代码。为了代码的可读性,iOS提供了诸如xib 和 storyboard的工具来拖拽控件,设置约束。但是如果我们需要设置一些圆角,富文本字体颜色等属性时,发现Xcode提供的属性列表是没有的。大概可以通过User Defined Runtime Attributes,这和我们所述的***IBInspectable*** 有关,但是呢不够优雅。

IBInspectable

You can add the IBInspectable attribute to any property in a class declaration, class extension, or category of type: boolean, integer or floating point number, string, localized string, rectangle, point, size, color, range, and nil.

我们可以在class, extenstion, category中添加 Inspectable 来修饰boolean, integer or floating point number, string, localized string, rectangle, point, size, color, range, and nil 类型的属性。

Objc中优雅添加属性

  1. 此处以添加圆角为例
  2. Category扩展属性
  3. 代码实例
@interface UIView (JJIBProp)

/// 圆角半径
@property (nonatomic, assign, readwrite) IBInspectable CGFloat cornerRadius;
/// statusbar 是否隐藏
@property (nonatomic, assign, readwrite) IBInspectable BOOL jjPreferedStatubarHidden;

@end

#import "UIView+JJIBProp.h"

@implementation UIView (JJIBProp)

- (void)setCornerRadius:(CGFloat)cornerRadius {
    self.layer.cornerRadius = cornerRadius;
    self.layer.masksToBounds = cornerRadius > 0;
}

- (CGFloat)cornerRadius {
    return self.layer.cornerRadius;
}

- (BOOL)jjPreferedStatubarHidden {
    return self.jjPreferedStatubarHidden;
}

- (void)setJjPreferedStatubarHidden:(BOOL)jjPreferedStatubarHidden {
    [UIStatusBarManager setShouldGroupAccessibilityChildren:jjPreferedStatubarHidden];
}

Swift

extension UIView {
    @IBInspectable var cornerRadius: CGFloat {
        get {
            return layer.cornerRadius
        }
        set {
            layer.cornerRadius = newValue
            layer.masksToBounds = newValue > 0
        }
    }
}

IB中大概是这样式的

storyboard显示结果

IBDesginable

这玩意儿可以直接渲染视图,我是这么知道的
If you want to write code for a custom view that runs only in Interface Builder, call that code from the prepareForInterfaceBuilder method. For example, while designing an app that uses the iPhone camera, you might want to draw an image that represents what the camera might capture. Interface Builder calls the prepareForInterfaceBuilder method instead of the awakeFromNib method. If you want to share code between these methods, move that code to another method that you call from both of these methods.

扒拉一大堆大概是这个意思:在IB中运行是获取不到完整的啥下文的,你需要在prepareForInterfaceBuilder()方法中运行,该方法可以和其它方法一起编译,但只有当视图正在准备在 Interface Builder 显示时执行。

还有另外一种方式:

In Objective-C and Swift, you can use the preprocessor macro TARGET_INTERFACE_BUILDER to conditionally compile code for your custom view class.

#if !TARGET_INTERFACE_BUILDER
   // Run this code only in the app
#else
   // Run this code only in Interface Builder
#endif

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值