Search code examples
ioscalayerbezierquartz-core

Drawing alphabet E in ios


How to draw an alphabet E in ios using beizer path or something?

I tried with normal image for E but its varying according to screen resolution but it should not vary,so I am going to draw via coding.


Solution

  • You can create a custom view. (subclass of UIView). And change its draw rect method like this.

    - (void)drawRect:(CGRect)rect {
         [super drawRect:rect];
    
         CGContextRef context = UIGraphicsGetCurrentContext();
         CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
    
         CGContextSetLineWidth(context, 3.0);
    
         CGContextMoveToPoint(context, 0,0);
    
         CGContextAddLineToPoint(context, 0, 60);
    
         CGContextMoveToPoint(context, 0,0);
    
         CGContextAddLineToPoint(context, 30, 0);
    
         CGContextMoveToPoint(context, 0, 30);
    
         CGContextAddLineToPoint(context, 20, 30);
    
         CGContextMoveToPoint(context, 0, 60);
    
         CGContextAddLineToPoint(context, 30, 60);
    
         CGContextStrokePath(context);
    }
    

    you can change line color, width or any other properties as you want.

    Edit: For center,

    First define width of E character, let say;

    #define VERTICAL_LINE 40
    #define TOP_HORIZONTAL_LINE 40
    #define CENTER_HORIZONTAL_LINE 40
    #define BOTTOM_HORIZONTAL_LINE 40
    #define LINE_PIECE 20
    

    And drawRect method;

    - (void)drawRect:(CGRect)rect {
        [super drawRect:rect];
    
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
    
        CGContextSetLineWidth(context, 3.0);
    
    
        // assumed top and bottom lines are equal width
    
        CGFloat x, y;
        x = (self.frame.size.width - MAX(TOP_HORIZONTAL_LINE,CENTER_HORIZONTAL_LINE)) / 2;
        y = (self.frame.size.height - VERTICAL_LINE) / 2;
    
        CGContextMoveToPoint(context, x, y);
    
        CGContextAddLineToPoint(context, x, y + VERTICAL_LINE);
    
        CGContextMoveToPoint(context, x,y);
    
        CGContextAddLineToPoint(context, x+TOP_HORIZONTAL_LINE, y);
    
        CGContextMoveToPoint(context, x, y+LINE_PIECE);
    
        CGContextAddLineToPoint(context, x+CENTER_HORIZONTAL_LINE, y+LINE_PIECE);
    
        CGContextMoveToPoint(context, x, y+VERTICAL_LINE);
    
        CGContextAddLineToPoint(context, x + BOTTOM_HORIZONTAL_LINE, y + VERTICAL_LINE);
    
        // and now draw the Path!
        CGContextStrokePath(context);
    }