Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing a rectangle in UIView

I am trying to draw a transparent rectangle in my UIView which has a black border.

My code however creates a completely black rectangle. Here's my code so far:

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGRect rectangle = CGRectMake(0, 100, 320, 100);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 0.5);
    CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 0.5);
    CGContextFillRect(context, rectangle);

}
like image 727
Ashish Agarwal Avatar asked Feb 26 '13 06:02

Ashish Agarwal


People also ask

How do you draw a rectangle in Java?

Similarly, we will draw a rectangle on Java applet by two ways . By using the drawRect(int x, int y, int width, int height) or by drawing four lines joining the edges . Examples: We will draw a rectangle of height 200 and width 200 and At a position 100,100 on the applet.


2 Answers

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGRect rectangle = CGRectMake(0, 100, 320, 100);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 0.0);   //this is the transparent color
    CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 0.5);
    CGContextFillRect(context, rectangle);
    CGContextStrokeRect(context, rectangle);    //this will draw the border

}

the effect is like this (backgroundColor is blue)

enter image description here

like image 170
Guo Luchuan Avatar answered Oct 06 '22 00:10

Guo Luchuan


It will provide the rectangle/square by adjusting values of self frame (customized view or subclass of any class which is inherited from UIView) with transparency.

[self.layer setBorderWidth:1.0];
[self.layer setBorderColor:[[UIColor colorWithRed:0.10 green:0.45 blue:0.73 alpha:1.0] CGColor]];
[self.layer setCornerRadius:2.0];
[self.layer setShadowOffset:CGSizeMake(-2, -2)];
[self.layer setShadowColor:[[UIColor lightGrayColor] CGColor]];
[self.layer setShadowOpacity:0.5];
like image 3
Madhu Avatar answered Oct 06 '22 01:10

Madhu