首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IBDesignable视图绘制

IBDesignable视图绘制
EN

Stack Overflow用户
提问于 2015-05-24 08:00:05
回答 1查看 1.4K关注 0票数 1

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

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

*text 01图像*

*文本02_______ *

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

现在我想从Interface中设置字体和颜色,但是我得到了错误:

instance :未能呈现AvailableSeatView的实例:呈现视图花费的时间超过200 ms。您的绘图代码可能会受到性能低下的影响。

这里我的代码:

在AvailableSeatView.h中

代码语言:javascript
复制
#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中

代码语言:javascript
复制
@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视图呈现超时我试图覆盖initWithFrameinitWithCoder,但它仍然不起作用。

EN

回答 1

Stack Overflow用户

发布于 2015-06-01 16:27:14

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

样本工程

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30421293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档