搜索看看? 搜索 User Defined Runtime Attributes
来源:互联网 发布:志鸿优化系列赢在高考 编辑:程序博客网 时间:2024/05/18 15:26
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
- User Defined Runtime Attributes
- 2014资料整理--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的使用
- 看看咱们国人的手机搜索习惯
- .Net多线程编程—使用Visual Studio 2012进行调试
- 关于CC2530存储器映射的讨论
- iOS 10 开发适配系列 之 权限Crash问题
- 2016年个人总结社区版
- ubuntu上pycharm缺少libcupti.so
- 搜索看看? 搜索 User Defined Runtime Attributes
- skynet范例研究-服务端
- C++初步(3)
- UVALive - 4294 Shuffle 映射+取反+最大区间覆盖
- 2017年前思绪整理
- 九度 oj 题目1149:子串计算
- 源码安装PHP扩展mysqli
- 未完成呀
- 各种linux系统配置多IP方法整理