【问题标题】:IBDesignable View RenderingIBDesignable 视图渲染
【发布时间】:2015-05-24 08:00:05
【问题描述】:

我有像这张图片这样的自定义视图,它可以工作(我在 drawRect 中设置的文本字体和文本颜色)

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

*文本 01 图像 *

*文本 02_______ *

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

现在我想从 Interface Builde 中设置字体和颜色,但出现错误:

IB Designables:无法渲染 AvailableSeatView 的实例:渲染视图的时间超过 200 毫秒。您的绘图代码可能会遇到性能缓慢的问题。

这是我的代码:

在 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;

在 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

在谷歌搜索后,我找到了这篇文章:IBDesignable View Rendering times out 我尝试覆盖 initWithFrameinitWithCoder 但它仍然无法正常工作。

【问题讨论】:

    标签: ios xcode rendering ibdesignable


    【解决方案1】:

    基本上,将 DrawRect( ) 方法用于实时渲染目的是一个错误的概念。苹果为此提供了一个单独的方法(用于实时渲染目的)“- (void)prepareForInterfaceBuilder { }”。 但是,在这种情况下,您不能从 IB 设置 UIFont 属性。但是你可以设置 UIcolor。

    Sample Project

    【讨论】:

    • 我可以在AppleIf you need to create code for a custom view that runs only in Interface Builder, call that code from the method prepareForInterfaceBuilder.看到,但是调试后,它没有遇到这个方法,所以它不起作用
    猜你喜欢
    • 2015-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-24
    相关资源
    最近更新 更多