@IBDesignable和@IBInspectable
近期一直在看苹果公司提供的两本swift官方教程电子书,一部是《The Swift Programming Language》,还有一部是《Using Swift With Cocoa and Objective-C》。昨天正好看到第二部电子书的“Writing Swift Classes with Objective-C Behavior”这一节,当中讲述了关于实时渲染这一技术。
以下是摘抄的当中一段内容:
“Live Rendering
You can use two different attributes—@IBDesignable and @IBInspectable—to enable live, interactive custom view design in Interface Builder. When you create a custom view that inherits from the UIView class or the NSView class, you can add the @IBDesignable attribute just before the class declaration. After you add the custom view to Interface Builder (by setting the custom class of the view in the inspector pane), Interface Builder renders your view in the canvas. You can also add the @IBInspectable attribute to properties with types compatible with user defined runtime attributes. After you add your custom view to Interface Builder, you can edit these properties in the inspector. SWIFT @IBDesignable
class MyCustomView: UIView {
@IBInspectable var textColor: UIColor
@IBInspectable var iconHeight: CGFloat
/* ... */
}
” 摘录来自: Apple Inc. “Using Swift with Cocoa and Objective-C”。 iBooks. https://itunes.apple.com/cn/book/using-swift-cocoa-objective/id888894773? mt=11
其大意就是说。能够将自己定义的代码实时渲染到Interface Builder中。
而它们之间的桥梁就是通过两个指令来完毕。即@IBDesignable和@IBInspectable。我们通过@IBDesignable告诉Interface Builder这个类能够实时渲染到界面中,可是这个类必须是UIView或者NSView的子类。通过@IBInspectable能够定义动态属性,就可以在attribute inspector面板中可视化改动属性值。
话不多说,以下举一个简单的样例,这个样例自己定义一个UIView的子类,该子类拥有一个UIButton。
import UIKit @IBDesignable
class MyCustomView: UIView { @IBInspectable var buttonTitleColor: UIColor! // button title color
@IBInspectable var buttonTitle: String! // button title
@IBInspectable var buttonFrame: CGRect! // button frame var myButton: UIButton! override init(frame: CGRect) {
// init stored properties
buttonTitleColor = UIColor.redColor()
buttonTitle = "button title"
buttonFrame = CGRectMake(0, 0, 100, 50) myButton = UIButton(frame: buttonFrame)
myButton.setTitleColor(buttonTitleColor, forState: .Normal)
myButton.setTitle(buttonTitle, forState: .Normal) // call super initializer
super.init(frame: frame) // add button to self
addSubview(myButton) } required init(coder aDecoder: NSCoder) {
// init stored properties
buttonTitleColor = UIColor.redColor()
buttonTitle = "button title"
buttonFrame = CGRectMake(0, 0, 100, 50) myButton = UIButton(frame: buttonFrame)
myButton.setTitleColor(buttonTitleColor, forState: .Normal)
myButton.setTitle(buttonTitle, forState: .Normal) // call super initializer
super.init(coder: aDecoder) // add button to self
addSubview(myButton)
} override func layoutSubviews() {
// refresh button state through attribute inspector
myButton.setTitleColor(buttonTitleColor, forState: .Normal)
myButton.setTitle(buttonTitle, forState: .Normal)
} }
上图:
从图中能够看到,代码实时渲染到IB中了。
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG91bmFvYnVu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
另外,我们在attribute inspector中也能够看到,由指令@IBInspectable声明的属性也出如今了面板中,通过改动这些值能够动态改变界面的效果(须要实现layoutSubviews方法)
详细project一览:
@IBDesignable和@IBInspectable的更多相关文章
- Swift - @IBDesignable和@IBInspectable
前言: 用storyboard/xib搞项目时,一些属性在Interface Builder上时无法设置,比如常用的layer的一些属性cornerRadius,borderColor等 (有时没必须 ...
- swift 第十四课 可视化view: @IBDesignable 、@IBInspectable
以前应objctiew-c 写项目的时候,就知道有这两个关键字,现在用swift了.用法稍作改变,基本用法还是一致的 虽然使用这个之后,有时候会报错的非常的莫名其妙----(其实还是自己技术不够牛…… ...
- iOS @IBDesignable和@IBInspectable
http://www.tuicool.com/articles/JVNRBjY @IBDesignable和@IBInspectable 时间 2014-10-08 11:02:03 CSDN博客 ...
- 关于IB_DESIGNABLE / IBInspectable的那些事
前言 IB_DESIGNABLE / IBInspectable 这两个关键字是在WWDC 2014年”What’s New in Interface Builder”这个Session里面,用Swi ...
- 如何设计一个 iOS 控件?(iOS 控件完全解析) (转)
前言 一个控件从外在特征来说,主要是封装这几点: 交互方式 显示样式 数据使用 对外在特征的封装,能让我们在多种环境下达到 PM 对产品的要求,并且提到代码复用率,使维护工作保持在一个相对较小的范围内 ...
- [翻译]使用Swift在Xcode中创建自定义控件
使用Swift在Xcode中创建自定义控件 原文 IBDesignable and IBInspectable With IBDesignable and IBInspectable, develop ...
- ios Swift 特性
特性提供了关于声明和类型的更多信息.在Swift中有两类特性,用于修饰声明的以及用于修饰类型的.例如,required特性,当应用于一个类的指定或便利初始化器声明时,表明它的每个子类都必须实现那个初始 ...
- Swift互用性: 使用Objective-C特性编写Swift类(Swift 2.0版)-b
本节包括内容: 继承Objective-C的类(Inheriting from Objective-C Classes) 采用协议(Adopting Protocols) 编写构造器和析构器(Writ ...
- 如何设计一个 iOS 控件?(iOS 控件完全解析)
前言 一个控件从外在特征来说,主要是封装这几点: 交互方式 显示样式 数据使用 对外在特征的封装,能让我们在多种环境下达到 PM 对产品的要求,并且提到代码复用率,使维护工作保持在一个相对较小的范围内 ...
随机推荐
- XML数据的读取—数据库配置文件
数据库配置文件(config.xml) <?xml version="1.0" encoding="utf-8"?> <configurati ...
- hadoop2.4.1伪分布式搭建
1.准备Linux环境 1.0点击VMware快捷方式,右键打开文件所在位置 -> 双击vmnetcfg.exe -> VMnet1 host-only ->修改subnet ip ...
- rest简单实例
http://www.cnblogs.com/fredric/archive/2012/03/03/2378680.html http://www.thinksaas.cn/topics/0/153/ ...
- MySQL -A不预读数据库信息(use dbname 更快)
mysql数据库预读与不预读数据库信息(use dbname)—Reading table information for completion of table and column names ...
- PHP 运算符
本章节我们将讨论 PHP 中不同运算符的应用. 在 PHP 中,赋值运算符 = 用于给变量赋值. 在 PHP 中,算术运算符 + 用于把值加在一起. PHP 算术运算符 运算符 名称 描述 实例 结果 ...
- php魔法常量
有七个魔术常量它们的值随着它们在代码中的位置改变而改变.例如 __LINE__ 的值就依赖于它在脚本中所处的行来决定.这些特殊的常量不区分大小写,如下: 名称 说明 __LINE__ 文件中的当前行号 ...
- myeclipse插件安装
MyEclipse插件安装 安装1: HELP->MuEclipse Configuration Center.切换到SoftWare选项卡. 点击 add site 打开对话框,在对话框中na ...
- cocos2d-x创建新项目模板
1.起因 长期使用项目中自带的HelloWorldScene来创建模板工程,不知大家有木有感到厌烦? 我是个懒人,所以就弄了个新的模板工程.这样最起码可以不用每次都把HelloWorldScene删掉 ...
- A Bug's Life(hdu1829种类并查集)
题意:有一群虫子,现在给你一些关系,判断这些关心有没有错 思路:向量种类并查集,下面讲一下向量的种类并查集 本题的各个集合的关心有两种0同性,1异性,怎么判断有错, 1.先判断他们是否在一个集合,即父 ...
- 转自:http://blog.sina.com.cn/s/blog_86e874d30101e3d8.html(谢谢原文作者),Win7下安装CentOS 6.5双系统
经过一下午的折腾,终于在64位的Windows 7上面成功安装了CentOS 6.5(64bit)系统,中途因为硬盘分区的问题失败了一次.下面是安装过程: 在安装过程中借助了这篇文章的内容:http: ...