Can't see the draw text on my subview on objective c

143 views Asked by At

so I created a subview on my project and inside that subview, I want to draw just a simple text. I create a subview on my Main Story Board and reference it to my CustomView. Set its size equal to my custom view size. Here is the my snippet code.

The custom view does show up as red color, like I set how it display, however, the text inside it ("Hello world") doesn't show up. I've been looking at the code for a while now and could not figure out what I did wrong. I really need help. Would someone tell me what to do ? Thank you.

ViewController.h

@interface ViewController : UIViewController

@property(retain, nonatomic) UIView *subView;

@end

ViewController.m

@interface ViewController ()

@end

@implementation ViewController

- (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

@interface CustomView : UIView

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

@end

CustomView.m

    @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
1

There are 1 answers

0
Debanjan Chakraborty On

I tried your code, except using xibs and it works perfect. Btw, where did you placed your customView in your viewController.view?