User Defined Runtime Attributes

简介: <div id="text" style="word-break:break-word; width:570px; margin-top:10px; margin-right:auto; margin-left:auto; line-height:24px; font-size:14px; overflow:hidden; color:rgb(1,1,1); font-family:宋体,

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


添加在layer已layer开头,自己的属性直接添加。



目录
相关文章
|
8月前
|
XML C# 数据格式
C#报错 The ‘xmins‘ attribute is not supported in this context
System.Xml.Schema.XmlSchemaException:“The ‘xmins’ attribute is not supported in this context.”异常的解决百度翻译:System.Xml.Schema.XmlSchemaException:“此上下文中不支持”xmins“属性。”这是在使用System.Data.DataSet的ReadXml读取x...
55 0
C#报错 The ‘xmins‘ attribute is not supported in this context
|
11月前
Unknown custom element: <add-employee> - did you register the component correctly? For red cursive c
原因: 1.组件名没写对(导入和注册不对应) 2.components少写了个s 3.组件命名最好是驼峰命名 4.导入时语法错误 5.代码中有两个components,后一个的值把前一个覆盖了 6.组件直接循环套用了
|
JavaScript 算法 前端开发
Property xxx was accessed during render but is not defined on instance
目前el-form的model主要用表单验证的,也就是配合el-form的rules和el-form-item的prop来使用的。不信的话,你可以增加一个rules和prop(为了调用验证方法,也el-form也加一个ref属性,相当于id或者class选择器的意思),但是不写model,然后验证的话,会提示缺少model,导致无法验证成功。
Property xxx was accessed during render but is not defined on instance
|
开发者
报错__wxConfig is not defined
报错__wxConfig is not defined
213 0
报错__wxConfig is not defined
|
Java
The type XXX cannot be resolved. It is indirectly referenced from required .class files
The type XXX cannot be resolved. It is indirectly referenced from required .class files
93 0
|
Kotlin
Program type already present: org.intellij.lang.annotations.Flow\Program type already present: org.i
Program type already present: org.intellij.lang.annotations.Flow\Program type already present: org.i
109 0
Program type already present: org.intellij.lang.annotations.Flow\Program type already present: org.i
No injector for custom defined data type的解决办法
No injector for custom defined data type的解决办法
229 0
No injector for custom defined data type的解决办法
How to resolve warning message Access restriction -The type Resource is not accessible
How to resolve warning message Access restriction -The type Resource is not accessible
How to resolve warning message Access restriction -The type Resource is not accessible