Skip to content

Instantly share code, notes, and snippets.

@japaternoster
Created November 6, 2010 08:49
#import <Foundation/Foundation.h>
void drawLinearGradient(CGContextRef context, CGRect rect, CGColorRef startColor, CGColorRef endColor);
CGRect rectFor1PxStroke(CGRect rect);
void draw1PxStroke(CGContextRef context, CGPoint startPoint, CGPoint endPoint, CGColorRef color);
void draw1PxStrokeWithHighlight(CGContextRef context, CGPoint startPoint, CGPoint endPoint, CGColorRef color, CGColorRef highlight);
void drawGlossAndGradient(CGContextRef context, CGRect rect, CGColorRef startColor, CGColorRef endColor);
static inline double radians (double degrees) { return degrees * M_PI/180; }
CGMutablePathRef createArcPathFromBottomOfRect(CGRect rect, CGFloat arcHeight);
CGMutablePathRef createRoundedRectForRect(CGRect rect, CGFloat radius);
#import "Common.h"
CGRect rectFor1PxStroke(CGRect rect) {
return CGRectMake(rect.origin.x + 0.5, rect.origin.y + 0.5, rect.size.width - 1, rect.size.height - 1);
}
void drawLinearGradient(CGContextRef context, CGRect rect, CGColorRef startColor, CGColorRef endColor) {
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat locations[] = { 0.0, 1.0 };
NSArray *colors = [NSArray arrayWithObjects:(id)startColor, (id)endColor, nil];
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef) colors, locations);
CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));
CGContextSaveGState(context);
CGContextAddRect(context, rect);
CGContextClip(context);
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
CGContextRestoreGState(context);
CGGradientRelease(gradient);
}
void draw1PxStroke(CGContextRef context, CGPoint startPoint, CGPoint endPoint, CGColorRef color) {
CGContextSaveGState(context);
CGContextSetLineCap(context, kCGLineCapSquare);
CGContextSetStrokeColorWithColor(context, color);
CGContextSetLineWidth(context, 1.0);
CGContextMoveToPoint(context, startPoint.x + 0.5, startPoint.y + 0.5);
CGContextAddLineToPoint(context, endPoint.x + 0.5, endPoint.y + 0.5);
CGContextStrokePath(context);
CGContextRestoreGState(context);
}
void draw1PxStrokeWithHighlight(CGContextRef context, CGPoint startPoint, CGPoint endPoint, CGColorRef color, CGColorRef highlight) {
draw1PxStroke(context,startPoint,endPoint,color);
draw1PxStroke(context,CGPointMake(startPoint.x, startPoint.y+1.0),CGPointMake(endPoint.x, endPoint.y+1.0),highlight);
}
void drawGlossAndGradient(CGContextRef context, CGRect rect, CGColorRef startColor, CGColorRef endColor) {
drawLinearGradient(context, rect, startColor, endColor);
CGColorRef glossColor1 = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.35].CGColor;
CGColorRef glossColor2 = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.1].CGColor;
CGRect topHalf = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height/2);
drawLinearGradient(context, topHalf, glossColor1, glossColor2);
}
CGMutablePathRef createArcPathFromBottomOfRect(CGRect rect, CGFloat arcHeight) {
CGRect arcRect = CGRectMake(rect.origin.x, rect.origin.y + rect.size.height - arcHeight,
rect.size.width, arcHeight);
CGFloat arcRadius = (arcRect.size.height/2) + (pow(arcRect.size.width, 2) / (8*arcRect.size.height));
CGPoint arcCenter = CGPointMake(arcRect.origin.x + arcRect.size.width/2, arcRect.origin.y + arcRadius);
CGFloat angle = acos(arcRect.size.width / (2*arcRadius));
CGFloat startAngle = radians(180) + angle;
CGFloat endAngle = radians(360) - angle;
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddArc(path, NULL, arcCenter.x, arcCenter.y, arcRadius, startAngle, endAngle, 0);
CGPathAddLineToPoint(path, NULL, CGRectGetMaxX(rect), CGRectGetMinY(rect));
CGPathAddLineToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMinY(rect));
CGPathAddLineToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMaxY(rect));
return path;
}
CGMutablePathRef createRoundedRectForRect(CGRect rect, CGFloat radius) {
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, CGRectGetMidX(rect), CGRectGetMinY(rect));
CGPathAddArcToPoint(path, NULL, CGRectGetMaxX(rect), CGRectGetMinY(rect),
CGRectGetMaxX(rect), CGRectGetMaxY(rect), radius);
CGPathAddArcToPoint(path, NULL, CGRectGetMaxX(rect), CGRectGetMaxY(rect),
CGRectGetMinX(rect), CGRectGetMaxY(rect), radius);
CGPathAddArcToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMaxY(rect),
CGRectGetMinX(rect), CGRectGetMinY(rect), radius);
CGPathAddArcToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMinY(rect),
CGRectGetMaxX(rect), CGRectGetMinY(rect), radius);
CGPathCloseSubpath(path);
return path;
}
#import <UIKit/UIKit.h>
#import "SBQuestion.h"
#import "SBAnswer.h"
@interface SBQuestionView : UIView {
int surveyQuestionNumber;
SBQuestion *currentQuestion;
UILabel *questionTitleLabel;
UILabel *questionHintLabel;
}
@property (nonatomic, assign) int surveyQuestionNumber;
@property (nonatomic, retain) SBQuestion *currentQuestion;
@property (nonatomic, retain) UILabel *questionTitleLabel;
@property (nonatomic, retain) UILabel *questionHintLabel;
- (id)initWithQuestion:(SBQuestion*)question number:(int)questionNo;
@end
#import "SBQuestionView.h"
#import "Common.h"
@implementation SBQuestionView
@synthesize surveyQuestionNumber;
@synthesize currentQuestion;
@synthesize questionTitleLabel;
@synthesize questionHintLabel;
- (id)initWithQuestion:(SBQuestion*)question number:(int)questionNo {
if ((self = [super initWithFrame:CGRectMake(0, 0, 768, 768)])) {
[self setBackgroundColor:[UIColor colorWithRed:176.0/255.0 green:224.0/255.0 blue:230.0/255.0 alpha:1.0]];
[self setSurveyQuestionNumber:questionNo];
currentQuestion = [[SBQuestion alloc] initWithQuestion:question];
NSLog(@"Current Question: %@",currentQuestion);
UILabel *questionNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 40, 200, 50)];
[questionNumberLabel setBackgroundColor:[UIColor clearColor]];
[questionNumberLabel setText:[NSString stringWithFormat:@"Question %i", surveyQuestionNumber]];
[questionNumberLabel setTextAlignment:UITextAlignmentCenter];
[questionNumberLabel setFont:[UIFont boldSystemFontOfSize:28.0]];
[questionNumberLabel setTextColor:[UIColor whiteColor]];
[questionNumberLabel setShadowOffset:CGSizeMake(0, -1.0)];
[questionNumberLabel setShadowColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5]];
[self addSubview:questionNumberLabel];
[questionNumberLabel release];
questionTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100.0, 110.0, 568.0, 100.0)];
[questionTitleLabel setBackgroundColor:[UIColor clearColor]];
[questionTitleLabel setText:[currentQuestion questionTitle]];
[questionTitleLabel setFont:[UIFont systemFontOfSize:30.0]];
[questionTitleLabel setTextAlignment:UITextAlignmentCenter];
[questionTitleLabel setTextColor:[UIColor grayColor]];
[questionTitleLabel setShadowColor:[UIColor whiteColor]];
[questionTitleLabel setShadowOffset:CGSizeMake(0, -1.0)];
[questionTitleLabel setLineBreakMode:UILineBreakModeWordWrap];
[questionTitleLabel setNumberOfLines:0];
[self addSubview:questionTitleLabel];
questionHintLabel = [[UILabel alloc] initWithFrame:CGRectMake(100.0, 215.0, 568.0, 30.0)];
[questionHintLabel setBackgroundColor:[UIColor clearColor]];
[questionHintLabel setText:[currentQuestion questionHint]];
[questionHintLabel setFont:[UIFont italicSystemFontOfSize:15.0]];
[questionHintLabel setTextAlignment:UITextAlignmentCenter];
[questionHintLabel setTextColor:[UIColor grayColor]];
[questionHintLabel setShadowColor:[UIColor whiteColor]];
[questionHintLabel setShadowOffset:CGSizeMake(0, -1.0)];
[self addSubview:questionHintLabel];
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorRef whiteColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0].CGColor;
CGColorRef lightGrayColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0].CGColor;
CGColorRef shadowColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.5].CGColor;
CGColorRef lightGreenColor = [[UIColor colorWithRed:158.0/255.0 green:192.0/255.0 blue:72.0/255.0 alpha:1.0] CGColor];
CGColorRef darkGreenColor = [[UIColor colorWithRed:102.0/255.0 green:142.0/255.0 blue:66.0/255.0 alpha:1.0] CGColor];
CGColorRef shadowGreenColor = [[UIColor colorWithRed:71.0/255.0 green:100.0/255.0 blue:66.0/255.0 alpha:1.0] CGColor];
CGFloat outerMargin = 20.0f;
CGRect outerRect = CGRectInset(self.bounds, outerMargin, outerMargin);
CGMutablePathRef outerPath = createRoundedRectForRect(outerRect, 15.0);
CGContextSaveGState(context);
CGContextSetFillColorWithColor(context, whiteColor);
CGContextSetShadowWithColor(context, CGSizeMake(0, 5), 5.0, shadowColor);
CGContextAddPath(context, outerPath);
CGContextFillPath(context);
CGContextRestoreGState(context);
CGContextSaveGState(context);
CGContextAddPath(context, outerPath);
CGContextClip(context);
drawLinearGradient(context, outerRect, whiteColor, lightGrayColor);
CGContextRestoreGState(context);
CGContextSaveGState(context);
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, whiteColor);
CGContextAddPath(context, outerPath);
CGContextStrokePath(context);
CGContextRestoreGState(context);
CGRect ribbonyRect = CGRectMake(10, 40, 220, 50);
CGContextSaveGState(context);
CGMutablePathRef ribbonPath = CGPathCreateMutable();
CGPathMoveToPoint(ribbonPath, NULL, ribbonyRect.origin.x, ribbonyRect.origin.y);
CGPathAddLineToPoint(ribbonPath, NULL, ribbonyRect.origin.x+ribbonyRect.size.width, ribbonyRect.origin.y);
CGPathAddLineToPoint(ribbonPath, NULL, ribbonyRect.origin.x+ribbonyRect.size.width-20, ribbonyRect.origin.y+((ribbonyRect.size.height)/2));
CGPathAddLineToPoint(ribbonPath, NULL, ribbonyRect.origin.x+ribbonyRect.size.width, ribbonyRect.origin.y+ribbonyRect.size.height);
CGPathAddLineToPoint(ribbonPath, NULL, ribbonyRect.origin.x, ribbonyRect.origin.y+ribbonyRect.size.height);
CGPathCloseSubpath(ribbonPath);
CGContextSaveGState(context);
CGContextSetShadowWithColor(context, CGSizeMake(0, 5), 5.0, shadowColor);
CGContextAddPath(context, ribbonPath);
CGContextFillPath(context);
CGContextRestoreGState(context);
CGContextAddPath(context, ribbonPath);
CGContextClip(context);
drawLinearGradient(context, ribbonyRect, lightGreenColor, darkGreenColor);
CGContextRestoreGState(context);
CGContextSaveGState(context);
CGContextAddPath(context, ribbonPath);
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, lightGreenColor);
CGContextStrokePath(context);
CGContextRestoreGState(context);
CGContextSaveGState(context);
CGContextAddPath(context, ribbonPath);
CGContextSetLineWidth(context, 1.5);
CGContextSetStrokeColorWithColor(context, darkGreenColor);
CGContextStrokePath(context);
CGContextRestoreGState(context);
CGContextMoveToPoint(context, ribbonyRect.origin.x, ribbonyRect.origin.y+ribbonyRect.size.height+1.5);
CGContextAddLineToPoint(context, 19.0, ribbonyRect.origin.y+ribbonyRect.size.height+1.5);
CGContextAddLineToPoint(context, 19.0, ribbonyRect.origin.y+ribbonyRect.size.height+12.0);
CGContextSetFillColorWithColor(context, shadowGreenColor);
CGContextFillPath(context);
CFRelease(ribbonPath);
CFRelease(outerPath);
}
- (void)dealloc {
[currentQuestion release], currentQuestion = nil;
[questionTitleLabel release], questionTitleLabel = nil;
[questionHintLabel release], questionHintLabel = nil;
[super dealloc];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment