User Defined Runtime Attributes
来源:互联网 发布:淘宝商城口红 编辑:程序博客网 时间:2024/05/18 13:04
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
- User Defined Runtime Attributes
- User Defined Runtime Attributes
- User Defined Runtime Attributes
- User Defined Runtime Attributes
- User Defined Runtime Attributes
- User Defined Runtime Attributes
- 2014资料整理--User Defined Runtime Attributes
- 搜索看看? 搜索 User Defined Runtime Attributes
- xib中User Defined Runtime Attributes使用
- Expanding User-Defined Runtime Attributes in Xcode with Objective-C
- iOS User Defined Runtime Attributes to define the key path
- StoryBoard之User Defined Runtime Attributes的使用
- Pushing the Limits of User-Defined Runtime Attributes in Interface Builder
- User Defined Runtime Attributes在xib文件中给组件添加圆角
- iOS 在user defined runtime attributes(用户定义的运行时属性)改变UI
- 关于User Defined Runtime Attributes的使用,Xib定制view的边框、颜色etc
- [iOS]用Xib编辑视图时User Defined Runtime Attributes的使用
- oracle:System defined event attributes
- Android L 64位兼容32 应用程序的认识
- 【python】os文件操作模块
- java编程面试题
- poj_3436
- 监听APP是否切换到了后台
- User Defined Runtime Attributes
- Python模拟switch
- birt初学中遇到为问题
- linux 配置如何识别U盘
- 实现数的反转
- 记录一下seekbar遇到的问题
- KM算法 详解+模板
- Ubuntu 14.04 64bit上使用IPython玩转Docker
- Android Shape Drawable 说明