0

I'm trying to draw an E shaped thingy and as far as I know there are 2 ways to go one of them being path.

I could draw with path as the below documentation suggests. https://developer.apple.com/library/ios/documentation/2DDrawing/Conceptual/DrawingPrintingiOS/BezierPaths/BezierPaths.html

Or I could create a rectangle with a UIView and carve it with 2 small squares and make an E by substracting those 2 spots.

I'm not sure which way is the way to go considering efficiency and everything. If there's any other better ways, please enlighten me. Thanks!

(I've found plenty about drawing shapes on here but none is even remotely recent and I'd like a recent answer)

durazno
  • 559
  • 2
  • 7
  • 25

2 Answers2

1

One of few possible ways:

  1. Create a subclass of UIView.
  2. Set CAShapeLayer as a backing layer your UIView.
  3. Configure UIBezierPath by adding lines to reflect your E-shape.
  4. Assign UIBezierPath CGPath property to CAShapeLayer's path property.
  5. Add your view to view hierarch.

Other:

  1. Create a subclass of UIView.
  2. Override drawRect method.
  3. Add shapes/lines to drawing context to reflect your E-shape.
  4. Add your view to view hierarch.

IMHO first solution will be bit more efficient, but I usually goes into second one.

psci
  • 903
  • 9
  • 18
0

If you don't want to use bezier path, you can draw the letter on view:

    CGContextRef context = UIGraphicsGetCurrentContext();

    // Text Drawing
    CGRect textRect = CGRectMake(CGRectGetMinX(frame) + 52, CGRectGetMinY(frame) + 26, 17, 21);
    {
        NSString* textContent = @"E"; // You can draw any letter , just replace this!
        NSMutableParagraphStyle* textStyle = NSMutableParagraphStyle.defaultParagraphStyle.mutableCopy;
        textStyle.alignment = NSTextAlignmentCenter;

        NSDictionary* textFontAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize: UIFont.labelFontSize], NSForegroundColorAttributeName: UIColor.blackColor, NSParagraphStyleAttributeName: textStyle};

        CGFloat textTextHeight = [textContent boundingRectWithSize: CGSizeMake(textRect.size.width, INFINITY)  options: NSStringDrawingUsesLineFragmentOrigin attributes: textFontAttributes context: nil].size.height;
        CGContextSaveGState(context);
        CGContextClipToRect(context, textRect);
        [textContent drawInRect: CGRectMake(CGRectGetMinX(textRect), CGRectGetMinY(textRect) + (CGRectGetHeight(textRect) - textTextHeight) / 2, CGRectGetWidth(textRect), textTextHeight) withAttributes: textFontAttributes];
        CGContextRestoreGState(context);
    }

Subclass the UIVIew and include this code in the drawRect method.

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109