ios 在界面生成器中设置 UIButton 图层边框宽度和颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26161429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set UIButton Layer Border Width and Color in Interface Builder
提问by raginggoat
Can I use IB_DESIGNABLE and/or IBInspectable to set layer.borderWidth and layer.borderColor in Interface Builder? I am currently creating my button in code but I'd like to be able to set all of this in IB but I'm not sure if these properties can be set that way in Xcode 6. I'd like to make this an IBOutlet instead of having all of this set in code. Here is my button code now.
我可以使用 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];
I set these values as suggested and the border is never shown in the simulator. EDIT: I found out why the border wasn't showing up when setting these values in IB. The border color is a CGColor so I had to set it in code.
我按照建议设置了这些值,并且边界从未显示在模拟器中。编辑:我发现了为什么在 IB 中设置这些值时没有显示边框。边框颜色是 CGColor 所以我必须在代码中设置它。
回答by Baig
Actually you can set some properties of a view's layer through interface builder. I know that I can set a layer's borderWidth and cornerRadius through xcode. borderColor doesn't work, probably because the layer wants a CGColor instead of a UIColor.
实际上,您可以通过界面构建器设置视图层的一些属性。我知道我可以通过xcode设置图层的borderWidth和cornerRadius。borderColor 不起作用,可能是因为该层需要 CGColor 而不是 UIColor。
You might have to use Strings instead of numbers, but it works!
您可能必须使用字符串而不是数字,但它有效!
But you can use categories to proxy properties such as layer.borderColor. (From the ConventionalCCocoaPod)
但是您可以使用类别来代理属性,例如 layer.borderColor。(来自ConventionalCCocoaPod)
CALayer+XibConfiguration.h:
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:
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
The result will be apparent during runtime, not in Xcode.
结果将在运行时显而易见,而不是在 Xcode 中。
回答by Istvan
You can set most of those in the interface builder adding runtime attributes to the elements:
您可以在界面构建器中设置大部分内容,向元素添加运行时属性:
For layer.borderWidth = 2.0f; would be:
对于 layer.borderWidth = 2.0f; 将是:
Select the button and add a new attribute
选择按钮并添加新属性
keypath : layer.borderWidth
关键路径:layer.borderWidth
type: Number Value 2
类型:数值 2
These changes will not be visible inside the interface builder, only at runtime
这些更改在界面构建器中不可见,仅在运行时可见
回答by sreekanthk
yes u can
in the right side click on identity inspector, u will find like this
是的,您可以在右侧单击身份检查器,您会发现这样
click +
in User Defined Runtime Attributes
点击+
进入User Defined Runtime Attributes
select keypath
and edit it
选择keypath
并编辑它
write the code like this
像这样写代码
layer.cornerRadius
and in Type
change the type to number
and set ur required value like this
layer.cornerRadius
并将Type
类型更改为number
并设置您所需的值,如下所示
u can also change text colors and so many.
您还可以更改文本颜色等等。
Happy coding
快乐编码