简体   繁体   中英

UIView with custom shape

I want to have a UIElement (any of UIView, UIButton, UILabel etc.) with custom shape lets say a triangle. How do you suggest to achieve it.
Thanks in advance.

Here is a sample of a UIView subclass (triangle):

@implementation TriangleView

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

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGContextBeginPath(ctx);

    CGContextMoveToPoint   (ctx, CGRectGetMinX(rect), CGRectGetMaxY(rect));  // top left
    CGContextAddLineToPoint(ctx, CGRectGetMidX(rect), CGRectGetMinY(rect));  // mid right
    CGContextAddLineToPoint(ctx, CGRectGetMaxX(rect), CGRectGetMaxY(rect));  // bottom left

    CGContextClosePath(ctx);

    CGContextSetRGBFillColor(ctx, 241/255.0, 241/255.0, 241/255.0, 1);
    CGContextFillPath(ctx);

}

@end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM