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.