#import "MJPlaceholderView.h" @implementation MJPlaceholderView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self != nil) { // This is run when the view is created // in Interface Builder. } return self; } - (void)prepareForInterfaceBuilder { [super prepareForInterfaceBuilder]; /* * If you need to create code for a custom view that runs * only in Interface Builder, call that code from the method * prepareForInterfaceBuilder. For example, while designing * an app that uses the iPhone camera, you might want to draw * an image that represents what the camera might capture. * Although it’s compiled for runtime, code called from * prepareForInterfaceBuilder never gets called except by * Interface Builder at design time. */ } - (CGFloat)cornerRadius { return self.layer.cornerRadius; } - (void)setCornerRadius:(CGFloat)cornerRadius { self.layer.cornerRadius = cornerRadius; self.layer.masksToBounds = cornerRadius > 0.0f; } - (void)drawRect:(CGRect)rect { #if TARGET_INTERFACE_BUILDER // This is only compiled for the interface builder. #else // This is only compiled for runtime. #endif [(_fillColor ?: [UIColor magentaColor]) set]; UIRectFill(self.bounds); NSString *size = [NSString stringWithFormat:@"%u x %u", (uint32_t)self.bounds.size.width, (uint32_t)self.bounds.size.height]; [self drawCenteredText:size verticalOffsetInMultipleOfHeight:0.5f]; [self drawCenteredText:(self.text ?: @"Placeholder") verticalOffsetInMultipleOfHeight:-0.5f]; } - (void)drawCenteredText:(NSString *)text verticalOffsetInMultipleOfHeight:(CGFloat)offsetMultiple { CGFloat fontSize = _textSize > 1.0f ? _textSize : 16.0f; NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:fontSize], NSForegroundColorAttributeName:_textColor ?: [UIColor blackColor]}; CGSize size = [text sizeWithAttributes:attributes]; CGRect rect = self.bounds; CGRect r = CGRectMake(rect.origin.x + (rect.size.width - size.width) / 2.0, rect.origin.y + (rect.size.height - size.height) / 2.0 + size.height * offsetMultiple, rect.size.width, size.height); [text drawInRect:r withAttributes:attributes]; } @end