当前位置: 移动技术网 > 移动技术>移动开发>IOS > Storyboard和xib中添加属性(圆角,前景色...) IBInspectable和IBDesignable

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

2020年10月30日  | 移动技术网移动技术  | 我要评论
这里写自定义目录标题缘起IBInspectable`Objc`中优雅添加属性SwiftIB中大概是这样式的IBDesginable缘起iOS开发中,工程师会接触到大量的视图呈现代码。为了代码的可读性,iOS提供了诸如xib 和 storyboard的工具来拖拽控件,设置约束。但是如果我们需要设置一些圆角,富文本字体颜色等属性时,发现Xcode提供的属性列表是没有的。大概可以通过User Defined Runtime Attributes,这和我们所述的***IBInspectable*** 有关,但是

缘起

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

本文地址:https://blog.csdn.net/github_34552693/article/details/109383863

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网