rag*_*oat 17 objective-c interface-builder uibutton ios xcode6
我可以使用IB_DESIGNABLE和/或IBInspectable在Interface Builder中设置layer.borderWidth和layer.borderColor吗?我目前正在代码中创建我的按钮,但我希望能够在IB中设置所有这些,但我不确定这些属性是否可以在Xcode 6中设置.我想将它设置为IBOutlet而不是在代码中设置所有这些.这是我的按钮代码.
directions = [UIButton buttonWithType:UIButtonTypeRoundedRect];
directions.titleLabel.textAlignment = NSTextAlignmentCenter;
directions.titleLabel.font = [UIFont fontWithName:@"FranklinGothicStd-ExtraCond" size:20.0];
[directions setTitle:@"Directions" forState:UIControlStateNormal];
[directions setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
directions.frame = CGRectMake(20, 178, 70, 70);
directions.layer.borderWidth = 2.0f;
directions.layer.borderColor = [UIColor whiteColor].CGColor;
directions.clipsToBounds = YES;
directions.backgroundColor = [UIColor clearColor];
[directions addTarget:self action:@selector(getDirections:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:directions];
Run Code Online (Sandbox Code Playgroud)
我按照建议设置了这些值,并且模拟器中从不显示边框.编辑:我发现为什么在IB中设置这些值时边框没有显示出来.边框颜色是CGColor,所以我必须在代码中设置它.
Bai*_*aig 31
实际上,您可以通过界面构建器设置视图图层的某些属性.我知道我可以通过xcode设置图层的borderWidth和cornerRadius.borderColor不起作用,可能是因为该图层需要CGColor而不是UIColor.
您可能必须使用字符串而不是数字,但它可以工作!
但您可以使用类别代理属性,例如layer.borderColor.(来自ConventionalC CocoaPod)
CALayer的+ XibConfiguration.h:
#import <QuartzCore/QuartzCore.h>
#import <UIKit/UIKit.h>
@interface CALayer(XibConfiguration)
// This assigns a CGColor to borderColor.
@property(nonatomic, assign) UIColor* borderUIColor;
@end
Run Code Online (Sandbox Code Playgroud)
CALayer的+ XibConfiguration.m:
#import "CALayer+XibConfiguration.h"
@implementation CALayer(XibConfiguration)
-(void)setBorderUIColor:(UIColor*)color
{
self.borderColor = color.CGColor;
}
-(UIColor*)borderUIColor
{
return [UIColor colorWithCGColor:self.borderColor];
}
@end
Run Code Online (Sandbox Code Playgroud)
结果将在运行时显而易见,而不是在Xcode中.
您可以在界面构建器中设置大多数添加运行时属性的元素:
对于layer.borderWidth = 2.0f; 将会:
选择按钮并添加新属性
keypath:layer.borderWidth
类型:数字值2
只有在运行时,这些更改才会在界面构建器中可见
是的,您可以在右侧单击身份检查器,您会发现这样
单击+
在User Defined Runtime Attributes
选择keypath
并编辑
像这样写代码
layer.cornerRadius
然后Type
将类型更改为number
并设置您所需的值
您还可以更改文本颜色等等。
快乐编码
归档时间: |
|
查看次数: |
60684 次 |
最近记录: |