jamie-wilson
jamie-wilson

Reputation: 1925

Drawing lines in UIView in Objective-C

This may be answered elsewhere, if so shame on me...

I am drawing a table in a UIView (which is added to a UITableViewcell), and I am currently laying out the text in rows and columns, but in terms of the borders, i'm using an image (a top line with corner radius, a bottom line, a vertical line and a horizontal line).

I want to draw these using the CGContextMoveToPoint code, but is it possible to specify the UIView to use? In the method everything is being added to a dynamically created view, I want to draw lines into that view before returning it.

It seems like this wouldn't work overriding the drawRect?

ie in pseudo code:

-(UIView *)myView {
    UIView *myView = [[UIView alloc] initWithFrame:CG.....];
    foreach(NSDictionary *d in rowArray) {
        //draw left column text
        //draw right column text
    }
    //This is what i want to do?
    CGContextRef currentContext = UIGraphicsGetCurrentContext(myView?????);
}

Upvotes: 0

Views: 1648

Answers (3)

hotpaw2
hotpaw2

Reputation: 70673

For stuff you just want to draw once, you can malloc a bitmap of the appropriate size, use that bitmap to create a CGBitmapContext, draw into that context, create an image from that context, and assign that CGImageRef to the CALayer of your UIView (or UIView overlay).

Upvotes: 0

Denis
Denis

Reputation: 6413

why not just creating a UIView subclass that will do the drawing stuff in drawRect: ? I believe it's not a good idea for placing a drawing code outside drawRect: method.

Upvotes: 2

Daniel
Daniel

Reputation: 31559

If you want to draw without an image in memory you have to do this with drawRect as it will be discarded every frame and not saved. if you want something saved you can create an image, paint to it, and add it to the view.

Upvotes: 4

Related Questions