首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在我的目标c子视图上看不到绘制文本

在我的目标c子视图上看不到绘制文本
EN

Stack Overflow用户
提问于 2014-11-11 11:00:20
回答 1查看 107关注 0票数 0

所以我在我的项目上创建了一个子视图,在这个子视图中,我只想绘制一个简单的文本。我在我的主故事板上创建了一个子视图,并将其引用到我的CustomView。将其大小设置为等于我的自定义视图大小。下面是我的代码片段。

自定义视图确实显示为红色,就像我设置的那样,但是,其中的文本("Hello world")不显示。我已经看了一段时间的代码了,我不知道我做错了什么。我真的需要帮助。有人能告诉我该怎么做吗?谢谢。

ViewController.h

代码语言:javascript
运行
复制
@interface ViewController : UIViewController

@property(retain, nonatomic) UIView *subView;

@end

ViewController.m

@接口ViewController ()

@end

@实现ViewController

代码语言:javascript
运行
复制
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

CustomView.h

代码语言:javascript
运行
复制
@interface CustomView : UIView

@property(retain, nonatomic)IBOutlet UIView* view;

@end

CustomView.m

代码语言:javascript
运行
复制
    @implementation CustomView


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    // how to draw text in here

    [super drawRect:rect];
    // Initialize a graphics context in IOS
    CGContextRef context = UIGraphicsGetCurrentContext();

    // Flip the coordinate
    CGContextTranslateCTM(context, 0, self.bounds.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    // ???????
    // Initialize string
    CFStringRef string = CFSTR("Hello world");

    // Initialize font
    CTFontRef font = CTFontCreateWithName((CFStringRef)@"Helvetica", 16.0f, nil);

    CFStringRef keys[] = {kCTFontAttributeName};
    CFTypeRef values[] = {font};

    CFDictionaryRef attributes = CFDictionaryCreate(kCFAllocatorDefault, (const void **) &keys, (const void **) &values, sizeof(keys) / sizeof(keys[0]), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);

    CFAttributedStringRef attrString = CFAttributedStringCreate(kCFAllocatorDefault, string, attributes);

    CFRelease(string);
    CFRelease(attributes);

    CTLineRef line = CTLineCreateWithAttributedString(attrString);

    // Set text position and draw the line into the graphics context

    CGContextSetTextPosition(context, 10, 10);

    CTLineDraw(line, context);
    CFRelease(line);
}

-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];

    if(self)
    {
        // 1. load the .xib file
        [[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:self options:nil];

        // 2. add the subview
        self.view.backgroundColor = [UIColor redColor];
        [self addSubview:self.view];
    }

    return self;
}


@end
EN

回答 1

Stack Overflow用户

发布于 2014-11-11 11:53:27

我试过你的代码,除了使用xib,它工作得很好。顺便说一句,你在viewController.view中把customView放在哪里了?

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

https://stackoverflow.com/questions/26856740

复制
相关文章

相似问题

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