在界面生成器中设置UIButton图层边框宽度和颜色
我可以使用IB_DESIGNABLE和/或IBInspectable在Interface Builder中设置layer.borderWidth和layer.borderColor吗? 我目前在代码中创build我的button,但我想能够在IB中设置所有这些,但我不确定是否可以在Xcode 6中设置这些属性。我想使这个IBOutlet而不是将所有这些设置在代码中。 这是我的button代码。
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];
我按照build议设置了这些值,并且边界在模拟器中从不显示。 编辑:我发现为什么在IB中设置这些值时没有显示边框。 边框的颜色是一个CGColor,所以我不得不在代码中设置它。
其实你可以通过界面构build器来设置视图层的一些属性。 我知道我可以通过xcode设置图层的borderWidth和cornerRadius。 borderColor不起作用,可能是因为图层想要一个CGColor而不是UIColor。
你可能不得不使用string而不是数字,但它的工作原理!
但是您可以使用类别来代理属性,如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
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
结果将在运行时显而易见,而不是在Xcode中。
您可以在界面构build器中将其中的大部分设置添加到元素的运行时属性:
对于layer.borderWidth = 2.0f; 将会:
selectbutton并添加一个新的属性
keypath:layer.borderWidth
键入:数字值2
只有在运行时才能在界面构build器中看到这些更改
是的,你可以在右侧点击身份检查员,你会发现这样的
单击User Defined Runtime Attributes
+
selectkeypath
并编辑它
像这样写代码
layer.cornerRadius
和in Type
将Type
更改为number
并像这样设置ur所需的值
你也可以改变文字颜色等等。
快乐的编码