StoryBoard之User Defined Runtime Attributes的使用
来源:互联网 发布:mac os教程 编辑:程序博客网 时间:2024/05/18 11:47
User Defined Runtime Attributes 是一个不被看重但功能非常强大的的特性,它能够配置一些你在interface builder 中不能配置的属性。当你希望在IB中作尽可能多得事情,这个特性能够帮助你编写更加轻量级的viewcontroller。
1.如何使用
当你使用interface builder是点击任意一个控件,转换到identity Inspector
图 1
2属性类型
下面是一些可用的Attribute Types与其相对应的数据类型
Boolean – BOOL
Number – NSNumber * or any numeric scalar, e.g. NSInteger
String – NSString *
Point – CGPoint
Size – CGSize
Rect – CGRect
Range – NSRange
Color – UIColor *
这里还有下面一些特殊的类型:
Nil- 就是设置这个属性为nil
Localized String - 一个在locale文件当中的键值,它会在你的 strings 文件中去匹配对应的本地化字符
demo
配置一个 view 下面的CALayer 的属性,一般我们都是在viewController里的viewDidLoad里写代码进行配置,现在如果你要设置
layer.borderWidth 和 layer.cornerRadius 你只需要如下所示:
编译运行如下:
如果说你要配置一个自定义的控件,你就可以在User Defined Runtime Attributes里配置你的自定义的属性了 如 你的自定义的Button 自定义了一个叫name的属性那可以
如果你要在代码中获取你的自定义属性,你就可以
id value = [sender valueForKey:@"name"];
Converting From Other Types
只要相应的attribute type代表的时同样的数据类型,那么他们就可以相互替换使用。例如:Point和Size他们地下其实都是同样的结构体{float a,float b}。
但是除了上述几种attribute type,你还可以使用其他的IB不支持数据类型吗?例如我要配置CALayer的 border coloer 和 shadow color,他们都是CGColorRef类型的,并不能直接在User Defined Runtime Attributes进行配置,但请看解决方案:
图5
为了兼容CALayer 的KVC ,你得给CALayer增加一个分类
@implementation CALayer (Additions)
- (void)setBorderColorFromUIColor:(UIColor *)color
{
self.borderColor = color.CGColor;
}
@end
Convert From String
这经常从NSString中转换成你所要的类型,例如 配置一个 UIEdgeInsets类型的属性,这个不能够被运行时属性支持,但可以考虑如下方案
@implementation UIScrollView (Additions)
- (void)setContentInsetFromString:(NSString *)contentInsetString
{
self.contentInset = UIEdgeInsetsFromString(contentInsetString);
}
@end
- StoryBoard之User Defined Runtime Attributes的使用
- User Defined Runtime Attributes
- User Defined Runtime Attributes
- User Defined Runtime Attributes
- User Defined Runtime Attributes
- User Defined Runtime Attributes
- User Defined Runtime Attributes
- xib中User Defined Runtime Attributes使用
- 关于User Defined Runtime Attributes的使用,Xib定制view的边框、颜色etc
- [iOS]用Xib编辑视图时User Defined Runtime Attributes的使用
- 2014资料整理--User Defined Runtime Attributes
- 搜索看看? 搜索 User Defined Runtime Attributes
- iOS 在user defined runtime attributes(用户定义的运行时属性)改变UI
- Expanding User-Defined Runtime Attributes in Xcode with Objective-C
- iOS User Defined Runtime Attributes to define the key path
- Pushing the Limits of User-Defined Runtime Attributes in Interface Builder
- User Defined Runtime Attributes在xib文件中给组件添加圆角
- Storyboard的使用之Segues
- Ubuntu中Vim 中文乱码解决方法
- java中从键盘输入的三种方法
- jQuery源码分析之ajaxTransport和ajaxPrefilters执行函数之inspectPrefiltersOrTransports
- Linux内核和用户空间应用程序的接口—系统调用
- set方法的调用在内存管理中
- StoryBoard之User Defined Runtime Attributes的使用
- hihoCoder挑战赛16 A—— 王胖浩与三角形
- SharePoint 2010 升级到SharePoint 2013的流程
- 大杂烩
- 大话数据结构——第一、二章
- 链表--查看链表是否含有环(不一样的方法)
- Core Data 入门
- MySQL修改配置 区分大小写和不区分大小写
- create tablespace