Skip to content

Commit

Permalink
Add support for IB_DESIGNABLE
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgarbarev committed Dec 11, 2014
1 parent 35f5bd0 commit 2508a61
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
6 changes: 6 additions & 0 deletions UIView+NestedXib.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@

#import <UIKit/UIKit.h>

IB_DESIGNABLE
@interface UIView (NestedXib)

+ (NSString *)xibName;

- (void)drawInterfaceBuilderRect:(CGRect)rect;

@end

//Put this into your custom class implementation
#define IB_DRAW - (void)drawRect:(CGRect)rect { [self drawInterfaceBuilderRect:rect]; }
46 changes: 32 additions & 14 deletions UIView+NestedXib.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,21 @@ @implementation UIView (NestedXib)
static NSMutableSet *loadingXibs;
static NSArray *propertiesToTransfer;

IMP originalImp;
id(*originalImp)(id, SEL, NSCoder *);

+ (void)load
{
IMP customImp = class_getMethodImplementation([UIView class], @selector(nested_xib_awakeAfterUsingCoder:));
Method originalMethod = class_getInstanceMethod([UIView class], @selector(awakeAfterUsingCoder:));

originalImp = method_setImplementation(originalMethod, customImp);
originalImp = (id(*)(id,SEL,NSCoder*))method_setImplementation(originalMethod, customImp);
}

+ (void)initialize
{
loadingXibs = [NSMutableSet new];

propertiesToTransfer = @[@"backgroundColor", @"frame", @"opaque", @"clipsToBounds", @"autoresizesSubviews",
@"autoresizingMask", @"hidden", @"clearsContextBeforeDrawing", @"tintColor", @"alpha",
@"exclusiveTouch", @"userInteractionEnabled", @"contentMode"];
propertiesToTransfer = @[@"frame", @"autoresizesSubviews", @"autoresizingMask", @"hidden", @"userInteractionEnabled", @"translatesAutoresizingMaskIntoConstraints"];
[super initialize];
}

Expand Down Expand Up @@ -61,18 +59,13 @@ - (id)nested_xib_awakeAfterUsingCoder:(NSCoder *)aDecoder
+ (NSString *)xibName
{
NSString *guessName = NSStringFromClass([self class]);
if ([[NSBundle mainBundle] pathForResource:guessName ofType:@"nib"]) {
if ([[NSBundle bundleForClass:[self class]] pathForResource:guessName ofType:@"nib"]) {
return guessName;
} else {
return nil;
}
}

+ (NSArray *)customProperties
{
return @[];
}

+ (BOOL)isLoadingXib:(NSString *)name
{
return [loadingXibs containsObject:name];
Expand All @@ -92,9 +85,7 @@ + (id)viewFromXib:(NSString *)xibName originalView:(UIView *)original

+ (void)transferPropertiesFromView:(UIView *)src toView:(UIView *)dst
{
//Transferring autolayout
dst.translatesAutoresizingMaskIntoConstraints = NO;

//Transferring autolayout
for (NSLayoutConstraint *constraint in src.constraints) {
BOOL replaceFirstItem = [constraint firstItem] == src;
BOOL replaceSecondItem = [constraint secondItem] == src;
Expand All @@ -113,5 +104,32 @@ + (void)transferPropertiesFromView:(UIView *)src toView:(UIView *)dst
}
}

- (void)drawInterfaceBuilderRect:(CGRect)rect
{
NSString *xibName = [[[self class] xibName] stringByAppendingPathExtension:@"xib"];

if (xibName) {

CGContextRef context = UIGraphicsGetCurrentContext();

[[UIColor colorWithWhite:0.93 alpha:1] setFill];

CGContextFillRect(context, self.bounds);

UILabel *label = [[UILabel alloc] initWithFrame:self.bounds];
label.lineBreakMode = NSLineBreakByWordWrapping;
label.numberOfLines = 0;
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor colorWithWhite:0.78 alpha:1];

UIFont *baseFont = [UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:33];
UIFont *subtitleFont = [baseFont fontWithSize:24];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"Nested Xib\n" attributes:@{NSFontAttributeName : baseFont}];
[string appendAttributedString:[[NSAttributedString alloc] initWithString:xibName attributes:@{NSFontAttributeName : subtitleFont}]];
label.attributedText = string;

[label drawRect:self.bounds];
}
}

@end

0 comments on commit 2508a61

Please sign in to comment.