简体   繁体   English

具有自定义形状的UIView

[英]UIView with custom shape

I want to have a UIElement (any of UIView, UIButton, UILabel etc.) with custom shape lets say a triangle. 我想要一个具有自定义形状的UIElement(UIView,UIButton,UILabel等中的任何一个),可以说一个三角形。 How do you suggest to achieve it. 您如何建议实现它。
Thanks in advance. 提前致谢。

Here is a sample of a UIView subclass (triangle): 这是一个UIView子类(三角形)的示例:

@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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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