Create triangle UIView

I need to create an example UIView as a tag:

enter image description here

The second option for me is to create a standard UIView (Square) and add another small triangular view to it.

But I don't know how to create this triangular look.

+3


source to share


3 answers


you can do it with UIBezierPath, this is sample code to use it. it won't look the same as your expected result, but you can paint with your dots



UIBezierPath* trianglePath = [UIBezierPath bezierPath];
[trianglePath moveToPoint:CGPointMake(0, view3.frame.size.height-100)];
[trianglePath addLineToPoint:CGPointMake(view3.frame.size.width/2,100)];
[trianglePath addLineToPoint:CGPointMake(view3.frame.size.width, view2.frame.size.height)];
[trianglePath closePath];

CAShapeLayer *triangleMaskLayer = [CAShapeLayer layer];
[triangleMaskLayer setPath:trianglePath.CGPath];

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0, size.width, size.height)];

view.backgroundColor = [UIColor colorWithWhite:.75 alpha:1];
view.layer.mask = triangleMaskLayer;
[self.view addSubview:view];

      

+1


source


If you want to create your own view, you can try this, you need to make it a class of your UIView.



- (void)drawRect:(CGRect)frame   
  {
//// Color Declarations
UIColor* color = [UIColor colorWithRed: 0.318 green: 0.472 blue: 0.866 alpha: 1];

//// Rectangle Drawing
UIBezierPath* rectanglePath = UIBezierPath.bezierPath;
[rectanglePath moveToPoint: CGPointMake(CGRectGetMinX(frame), CGRectGetMinY(frame) + 120)];
[rectanglePath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 171, CGRectGetMinY(frame) + 120)];
[rectanglePath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 240, CGRectGetMinY(frame) + 67)];
[rectanglePath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 240, CGRectGetMinY(frame) + 66)];
[rectanglePath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 167, CGRectGetMinY(frame))];
[rectanglePath addLineToPoint: CGPointMake(CGRectGetMinX(frame), CGRectGetMinY(frame))];
[rectanglePath addLineToPoint: CGPointMake(CGRectGetMinX(frame), CGRectGetMinY(frame) + 120)];
[rectanglePath closePath];
[color setFill];
[rectanglePath fill];
}

      

0


source


you can try this code and change the point for your requirements.

[[self view] setBackgroundColor:[UIColor blueColor]];

CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path,NULL,0.0,100);
CGPathAddLineToPoint(path, NULL, 160.0f, 100.0f);
CGPathAddLineToPoint(path, NULL, 160.0f, 100.0f);
CGPathAddLineToPoint(path, NULL, 200.0f, 150.0f);
CGPathAddLineToPoint(path, NULL, 160.0f, 200.0f);
CGPathAddLineToPoint(path, NULL, 0.0f, 200.0f);
CGPathAddLineToPoint(path, NULL, 0.0f, 480.0f);
CGPathAddLineToPoint(path, NULL, 0.0f, 0.0f);


CAShapeLayer *shapeLayer = [CAShapeLayer layer];
[shapeLayer setPath:path];
[shapeLayer setFillColor:[[UIColor redColor] CGColor]];
[shapeLayer setStrokeColor:[[UIColor blackColor] CGColor]];
[shapeLayer setBounds:CGRectMake(0.0f, 0.0f, 160.0f, 480)];
[shapeLayer setAnchorPoint:CGPointMake(0.0f, 0.0f)];
[shapeLayer setPosition:CGPointMake(0.0f, 0.0f)];
[[[self view] layer] addSublayer:shapeLayer];

CGPathRelease(path);

      

0


source







All Articles