Nav Link On

iPhone Dev Forums > General Development > SDK Coding Help > Problem to make a rounded rectangle... :(

Reply

 

LinkBack Thread Tools Display Modes
Old 08-01-2008, 06:44 AM   #1 (permalink)
Junior Member
Default Avatar
 
Join Date: Jul 2008
Posts: 26
Default Problem to make a rounded rectangle... :(

Hi all,

I am creating a rect using CGRectMake and creating a table view by using initWithFrame.

The problem is that it gives me a straight edge rectangle.

How do i create a rectangle with a round edge.

Any help would be appreciated.

Thanks,
Akshay Shah.
Akshay Shah is offline   Reply With Quote
Old 08-01-2008, 04:38 PM   #2 (permalink)
Administrator
 
MarkHernandez's Avatar
 
Join Date: Mar 2008
Location: San Diego, CA
Posts: 508
Default

Hi Akshay. I think we need more information. Do you want to draw a rounded rectangle graphic? Or do you want a table with rounded rectangle corners?
MarkHernandez is offline   Reply With Quote
Old 08-02-2008, 09:15 PM   #3 (permalink)
Member
Default Avatar
 
Join Date: Jun 2008
Posts: 56
Default

I'll just post this in case it's useful for someone else, but this is the code I wrote to add the missing CGContextAddRoundedRect function:

Code:

You must Login or Register to view and contribute code! This is done to increase participation in helping one another out, if you have been helped please pass on the favor.

void CGContextAddRoundedRect (CGContextRef c, CGRect rect, int corner_radius) {
	int x_left = rect.origin.x;
	int x_left_center = rect.origin.x + corner_radius;
	int x_right_center = rect.origin.x + rect.size.width - corner_radius;
	int x_right = rect.origin.x + rect.size.width;
	int y_top = rect.origin.y;
	int y_top_center = rect.origin.y + corner_radius;
	int y_bottom_center = rect.origin.y + rect.size.height - corner_radius;
	int y_bottom = rect.origin.y + rect.size.height;
	
	/* Begin! */
	CGContextBeginPath(c);
	CGContextMoveToPoint(c, x_left, y_top_center);
	
	/* First corner */
	CGContextAddArcToPoint(c, x_left, y_top, x_left_center, y_top, corner_radius);
	CGContextAddLineToPoint(c, x_right_center, y_top);
	
	/* Second corner */
	CGContextAddArcToPoint(c, x_right, y_top, x_right, y_top_center, corner_radius);
	CGContextAddLineToPoint(c, x_right, y_bottom_center);
	
	/* Third corner */
	CGContextAddArcToPoint(c, x_right, y_bottom, x_right_center, y_bottom, corner_radius);
	CGContextAddLineToPoint(c, x_left_center, y_bottom);
	
	/* Fourth corner */
	CGContextAddArcToPoint(c, x_left, y_bottom, x_left, y_bottom_center, corner_radius);
	CGContextAddLineToPoint(c, x_left, y_top_center);
	
	/* Done */
	CGContextClosePath(c);
	
}
My version has the Begin/Close path built in, for my own reasons. You can eliminate them if you want more control over the paths.

Here is how you would use it:

Code:

You must Login or Register to view and contribute code! This is done to increase participation in helping one another out, if you have been helped please pass on the favor.

CGContextSetRGBFillColor(c, 0, 1, 0, 1);
CGContextAddRoundedRect(c, CGRectMake(10,200,50,50), 10);
CGContextFillPath(c);
	
CGContextSetLineWidth(c, 3);
CGContextSetRGBStrokeColor(c, 0, 0, 0, 1);
CGContextAddRoundedRect(c, CGRectMake(10,200,50,50), 10);
CGContextStrokePath(c);
wyvwyv is offline   Reply With Quote
Old 08-04-2008, 05:17 AM   #4 (permalink)
Junior Member
Default Avatar
 
Join Date: Jul 2008
Posts: 26
Default

Hi,
Thanks for your replies...
Actually the thing i am doing is like i have a table view on top and a web view at bottom.
The webview is inititated with a frame(created using CGRectMake)
Now i want this Frame to have rounded edges..

Thanks,
Akshay.
Akshay Shah is offline   Reply With Quote
Old 05-23-2009, 01:32 AM   #5 (permalink)
Junior Member
Default Avatar
 
Join Date: May 2009
Posts: 1
Default

I took the code from above and modified it to do what you are looking for. I have essentially the same need for my application.

You can find the code on my blog at The Software Maven: Rounding Corners on a UIView - Travis Jensen's Software Blog.

Enjoy and thanks for the starting point on the drawing code!

tj
SoftwareMaven is offline   Reply With Quote
Reply

iPhone Dev Forums > General Development > SDK Coding Help


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0 RC1