Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7128103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:06:13+00:00 2026-05-28T11:06:13+00:00

so i need to draw 2 different lines. through another posting , i figured

  • 0

so i need to draw 2 different lines. through another posting, i figured out how to redraw a line. my question is, if i want to draw 2 lines do i need to have 2 code segments to do it? or is there a way to draw n lines?

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    NSLog(@"drawRect called");
    CGContextSetLineWidth(context, self.lineWidth);
    CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
    CGContextMoveToPoint(context, self.startPoint.x, self.startPoint.y);
    CGContextAddLineToPoint(context, self.endPoint.x, self.endPoint.y);
    CGContextStrokePath(context);
} 

implementation

draw2D *myCustomView = [[draw2D alloc] init];

myCustomView.startPoint = CGPointMake(0, 0);
myCustomView.endPoint = CGPointMake(300, 300);
myCustomView.lineWidth = 5;
myCustomView.lineColor = [UIColor redColor];

myCustomView.frame = CGRectMake(0, 0, 500, 500);
[myCustomView setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:myCustomView];
[myCustomView setNeedsDisplay];

myCustomView.endPoint = CGPointMake(100, 100);

myCustomView.lineColor = [UIColor orangeColor];
[myCustomView setNeedsDisplay];

this will just redraw the line. do i repeat the drawRect block for each line i want? so if i want two lines, do i have to do:

- (void)drawRect:(CGRect)rect
{
    //line1
    CGContextRef context1 = UIGraphicsGetCurrentContext();
    NSLog(@"drawRect called");
    CGContextSetLineWidth(context1, self.lineWidth1);
    CGContextSetStrokeColorWithColor(context1, self.lineColor1.CGColor);
    CGContextMoveToPoint(context1, self.startPoint1.x, self.startPoint1.y);
    CGContextAddLineToPoint(context1, self.endPoint1.x, self.endPoint1.y);
    CGContextStrokePath(context1);

    //line 2
    CGContextRef context2 = UIGraphicsGetCurrentContext();
    NSLog(@"drawRect called");
    CGContextSetLineWidth(context2, self.lineWidth2);
    CGContextSetStrokeColorWithColor(context2, self.lineColor2.CGColor);
    CGContextMoveToPoint(context2, self.startPoint2.x, self.startPoint2.y);
    CGContextAddLineToPoint(context2, self.endPoint2.x, self.endPoint2.y);
    CGContextStrokePath(context2);
} 

or is there a better way to handle drawing my lines?

EDIT: the end goal is to use it with a UIView with labels and textboxes i’m using as a form. i want lines to break up the form.

i’m wondering if i should just use these custom draw2D UIViews as lines only and place them on top of the UIView form, then i can just create multiple “draw2D” UIViews?

SOLUTION:

so here is the code i figured out that would work:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

        NSMutableArray *tempArray = [[NSMutableArray alloc] init];

        NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
        [tempDict setValue:[NSValue valueWithCGPoint:CGPointMake(10, 10)] forKey:@"start"];
        [tempDict setValue:[NSValue valueWithCGPoint:CGPointMake(500, 500)] forKey:@"end"];

        [tempArray addObject:tempDict];

        NSMutableDictionary *tempDict2 = [[NSMutableDictionary alloc] init];
        [tempDict2 setValue:[NSValue valueWithCGPoint:CGPointMake(400, 10)] forKey:@"start"];
        [tempDict2 setValue:[NSValue valueWithCGPoint:CGPointMake(500, 500)] forKey:@"end"];

        [tempArray addObject:tempDict2];

        self.pointArray = [NSArray arrayWithArray:tempArray];
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(context, self.lineWidth);
    CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);

    for (int i = 0; i < [self.pointArray count]; i++)
    {
        CGPoint tempPoint = [[[self.pointArray objectAtIndex:i] objectForKey:@"start"] CGPointValue];
        CGPoint tempPoint2 = [[[self.pointArray objectAtIndex:i] objectForKey:@"end"] CGPointValue];

        CGContextMoveToPoint(context, tempPoint.x, tempPoint.y);
        CGContextAddLineToPoint(context, tempPoint2.x, tempPoint2.y);
    }

    CGContextStrokePath(context);
}

i included my initWithFrame: method on creating the array that i used for testing (CGPoint’s are not objects, so had to figure out how to include them in an dictionary).

so this will loop through and create n lines.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T11:06:14+00:00Added an answer on May 28, 2026 at 11:06 am

    You can just repeat this bit as many times as you need (with different coordinates each time obviously).

    CGContextMoveToPoint(context, self.startPoint.x, self.startPoint.y);
    CGContextAddLineToPoint(context, self.endPoint.x, self.endPoint.y);
    

    The first line of code sets the start point of the line, the second line of code draws it. You don’t need to repeat all the rest of the code.

    In other words, you can draw multiple lines like this:

    //set up context
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, self.lineWidth1);
    CGContextSetStrokeColorWithColor(context, self.lineColor1.CGColor);
    
    //line1
    CGContextMoveToPoint(context, self.startPoint1.x, self.startPoint1.y);
    CGContextAddLineToPoint(context, self.endPoint1.x, self.endPoint1.y);
    
    //line 2
    CGContextMoveToPoint(context, self.startPoint2.x, self.startPoint2.y);
    CGContextAddLineToPoint(context, self.endPoint2.x, self.endPoint2.y);
    
    //line 3
    etc...
    
    //finished drawing
    CGContextStrokePath(context);
    

    You don’t need to copy the whole lot each time you draw a line, you can just repeat those two lines of code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to draw on a TPanel, ideally directly so I don't have another
I need to draw text of different sizes. however below code only gives choice
I have an application where I need to draw different graphics objects on the
I need to draw n different objects on a chart. I want to pick
I have a set of textures which I need to draw at different vertices
I need to draw a .png image on an UIView. I have no clue
I need to draw a UILabel striked through. Therefore I subclassed UILabel and implemented
I need to draw a line one inch long on any device given a
i need to draw two lines. can i use the same UIView subclass to
I need to draw several simple objects (polygons composed by lines and arcs, eventually

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.