1

I had custom view like this image and it work (text font and text color i set in drawRect)

+*******************+

*text 01 IMAGE *

*text 02_______ *

+*******************+

Now i wanna set font and color from Interface Builde but i get error:

IB Designables: Failed to render instance of AvailableSeatView: Rendering the view took longer than 200 ms. Your drawing code may suffer from slow performance.

Here my code:

In AvailableSeatView.h

#import <UIKit/UIKit.h>
IB_DESIGNABLE
@interface AvailableSeatView : UIView
@property (nonatomic) IBInspectable UIImage *image;
@property (nonatomic) IBInspectable NSInteger numberSeats;
@property (nonatomic) IBInspectable UIFont *numberSeatsFont;
@property (nonatomic) IBInspectable UIColor *numberSeatsColor;
@property (nonatomic) IBInspectable NSString *title;

In AvailableSeatView.m

@implementation AvailableSeatView
- (void)drawRect:(CGRect)rect {
    //UIImage
    CGRect myFrame = self.bounds;
    CGFloat yImage = myFrame.size.height/2 - self.image.size.height < 0 ? 0 : myFrame.size.height/2 - self.image.size.height;
    CGRect imageFrame = CGRectMake(myFrame.size.width/2, yImage, self.image.size.width, self.image.size.height);
    [self.image drawInRect:imageFrame];

    //UILabel number of seats
    [self drawString:[NSString stringWithFormat:@"%li", (long)self.numberSeats]
            withFont:self.numberSeatsFont
            andColor:self.numberSeatsColor
            aligment: NSTextAlignmentRight
              inRect:CGRectMake(0, yImage, imageFrame.origin.x, self.image.size.height)];

    //UILabel Title
    UIFont *titleFont = [UIFont systemFontOfSize:20];
    UIColor *titleColor = [UIColor redColor];
    [self drawString:self.title
            withFont:titleFont
            andColor:titleColor
            aligment: NSTextAlignmentCenter
              inRect:CGRectMake(0, myFrame.size.height/2, myFrame.size.width, myFrame.size.height/2)];
}

- (void) drawString: (NSString*) s
           withFont: (UIFont*) font
           andColor: (UIColor *)color
           aligment: (NSTextAlignment) alignment
             inRect: (CGRect) contextRect {

    CGFloat fontHeight = font.pointSize;
    CGRect textRect = CGRectMake(0, contextRect.origin.y, contextRect.size.width, fontHeight);
    NSMutableParagraphStyle* p = [NSMutableParagraphStyle new];
    p.alignment = alignment;
    [s drawInRect:textRect withAttributes:@{NSFontAttributeName: font,
                                            NSForegroundColorAttributeName: color,
                                            NSParagraphStyleAttributeName: p}];
}
@end

And after search in google, i find this post: IBDesignable View Rendering times out i try to do override initWithFrame, initWithCoder but it still not working.

Community
  • 1
  • 1
Trai Nguyen
  • 809
  • 1
  • 8
  • 22

1 Answers1

0

Basically, using DrawRect( ) method for the Live Rendering purpose is a wrong concept. Apple provides a separate method for this (For Live rendering purpose)"- (void)prepareForInterfaceBuilder { }". However you cannot set UIFont property from the IB in this case. But you can set UIcolor.

Sample Project

Kusal Shrestha
  • 1,643
  • 2
  • 13
  • 20
  • i can see in [Apple](https://developer.apple.com/library/ios/recipes/xcode_help-IB_objects_media/Chapters/CreatingaLiveViewofaCustomObject.html) `If you need to create code for a custom view that runs only in Interface Builder, call that code from the method prepareForInterfaceBuilder.` but after debug, it's not run into this method so that it not working – Trai Nguyen Jun 23 '15 at 08:46