Reputation: 3600
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.
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.
Upvotes: 20
Views: 65162
Reputation: 4995
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.
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 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
The result will be apparent during runtime, not in Xcode.
Upvotes: 35
Reputation: 362
yes u can
in the right side click on identity inspector, u will find like this
click +
in User Defined Runtime Attributes
select keypath
and edit it
write the code like this
layer.cornerRadius
and in Type
change the type to number
and set ur required value like this
u can also change text colors and so many.
Happy coding
Upvotes: 5
Reputation: 1627
You can set most of those in the interface builder adding runtime attributes to the elements:
For layer.borderWidth = 2.0f; would be:
Select the button and add a new attribute
keypath : layer.borderWidth
type: Number Value 2
These changes will not be visible inside the interface builder, only at runtime
Upvotes: 5