Objective-C(Swift) IBDesignable/IBInspectable
来源:互联网 发布:网络机顶盒如何使用 编辑:程序博客网 时间:2024/05/20 13:05
iOS8新特性IBDesignable/IBInspectable,利用运行时机制,可以把属性映射到XIB上了,同时我们UI类的自定义属性也可以映射上去,可以直接在XIB或者Storyboard中设置UI类的属性。
Objective-C实例:
.h中代码如下
@interface ZWView : UIView@property (nonatomic, assign)IBInspectable CGFloat cornerRadius;@property (nonatomic, assign)IBInspectable CGFloat bwidth;@property (nonatomic, assign)IBInspectable UIColor *bcolor;@end
.m中代码:
#import "ZWView.h"IB_DESIGNABLE@implementation ZWView- (void)setCornerRadius:(CGFloat)cornerRadius{ _cornerRadius = cornerRadius; self.layer.cornerRadius = _cornerRadius; self.layer.masksToBounds = YES;}- (void)setBcolor:(UIColor *)bcolor{ _bcolor = bcolor; self.layer.borderColor = _bcolor.CGColor;}- (void)setBwidth:(CGFloat)bwidth { _bwidth = bwidth; self.layer.borderWidth = _bwidth;}-(void)drawRect:(CGRect)rect{ NSString *drawString = @"www.baidu.com"; [drawString drawInRect:CGRectMake(20, 20, 180, 30) withFont:[UIFont systemFontOfSize:12.0]];}@end
在xib中拖入一个View,设置class为ZWView,设置完后便可以在xib中看到三个属性值
设置三个属性值后,效果如下:
Swift实例:
同样创建一个ZWView,代码如下:
import UIKit@IBDesignable class zwView: UIView { @IBInspectable var cornerRadius:CGFloat = 0.0 { didSet{ layer.cornerRadius = cornerRadius layer.masksToBounds = true } } @IBInspectable var borderColor:UIColor = UIColor(){ didSet{ layer.borderColor = borderColor.CGColor } } @IBInspectable var borderWith :CGFloat = 0.0{ didSet{ layer.borderWidth = borderWith } }}
在xib中拖入View,设置class和属性值
效果如下:
1 0
- Objective-C(Swift) IBDesignable/IBInspectable
- IBInspectable / IBDesignable (只能用于Swift?)
- Swift中@IBDesignable/@IBInspectable的使用
- swift_045(Swift @IBDesignable和@IBInspectable使用)
- IBInspectable / IBDesignable
- IBInspectable / IBDesignable
- IBInspectable / IBDesignable
- IBInspectable / IBDesignable
- IBInspectable / IBDesignable
- IBInspectable / IBDesignable
- IBInspectable / IBDesignable
- @IBDesignable和@IBInspectable
- @IBDesignable和@IBInspectable
- 自定义控件 @IBDesignable @IBInspectable
- IBOutLet/IBAction/IBDesignable/IBInspectable
- [IOS 开发] @IBDesignable @IBInspectable
- IBInspectable和IBDesignable
- IBInspectable / IBDesignable的使用方法详解
- 码流 / 码率 / 比特率 / 帧速率 / 分辨率 / 高清
- 安卓开发中内存问题分析(一)工具篇
- PHP获取今天、昨天、明天的日期
- xml方法封装
- 关于app跳转到系统设置的指定界面
- Objective-C(Swift) IBDesignable/IBInspectable
- apt-get couldn't be verified because the public key is not available: NO_PUBKEY XXX
- Java使用socket网络编程实现多人聊天室
- 工作笔记之复杂字符串的json解析,使用gson
- Can't compress a recycled bitmap的解决方案
- VirtualBox上Ubuntu Server虚拟机配置双网卡
- 每个程序员都必读的11篇文章
- [剑指offer:30]最小的k个数
- Android studio 使用心得(五)---代码混淆和破解apk