iOS 绘制View
2020-10-15 本文已影响0人
Rumbles
@implementation TimeStampView
@synthesize image;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
//self.clearsContextBeforeDrawing = NO;
//
labelStrokeColor = [UIColor whiteColor];//[UIColor blackColor];
labelStrokeWidth = 1;
labelFillColor = [UIColor whiteColor];//[UIColor grayColor];
labelFont = [UIFont fontWithName:@"Arial" size:13];
//
minorTickColor = [UIColor whiteColor];//[UIColor grayColor];//[UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:.7];
minorTickLength = frame.size.height*0.15;
minorTickWidth = 1;
//
majorTickColor = [UIColor whiteColor];//[UIColor grayColor];//[UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
majorTickLength = frame.size.height*0.38;
majorTickWidth = 1;
//
shadowColor = [UIColor whiteColor];//[UIColor grayColor];
shadowOffset = CGSizeMake(1.0,0.0);
shadowBlur = .5;
}
return self;
}
- (NSInteger)getSuperviewFrameWidth
{
return self.superview.frame.size.width ;
}
-(void)setDayCount:(NSInteger)iCount beginTime:(NSInteger)ibeginTime{
iDayCount = iCount;
m_iBeginUnixTime = ibeginTime;
}
-(void)setMinorTickDistance:(float)fDistance nubPerMajorTick:(NSInteger)iNum{
minorTickDistance = fDistance;
minorTicksPerMajorTick = iNum;
}
-(void)setLabelProperties:(UIColor *)color fillColor:(UIColor *)fillColor font:(UIFont *)font width:(CGFloat)width{
labelStrokeColor = color;
labelStrokeWidth = width;
labelFillColor = fillColor;
labelFont = font;
}
-(void)setMinorTickProperties:(UIColor *)color length:(CGFloat)len width:(CGFloat)width{
minorTickColor = color;
minorTickLength = len;
minorTickWidth = width;
}
-(void)setMajorTickProperties:(UIColor *)color length:(CGFloat)len width:(CGFloat)width{
majorTickColor = color;
majorTickLength = len;
majorTickWidth = width;
}
-(void)setShadowProperties:(UIColor *)color offset:(CGSize)offset blur:(CGFloat)fBlur{
shadowColor = color;
shadowOffset = offset;
shadowBlur = fBlur;
}
- (void)drawRect:(CGRect)rect {
return;
}
- (void)drawTicksWithContext:(CGContextRef)context atPoint:(CGPoint)point
{
float x = point.x;
if ([self isMajorTick:x]) {
[self drawMajorTickWithContext:context
atPoint:point
withColor:majorTickColor
width:majorTickWidth
length:majorTickLength];
int iNumber = 24;
if(m_Style == DayStyle){
iNumber = 1;
}else if (m_Style == HourStyle) {
iNumber = 24;
}
NSString *text = nil;
iIndexMinorTick = 0;
if ((iIndexMajorTick %iNumber) ==0 ){
// day
NSInteger unixTime = m_iBeginUnixTime+(iIndexMajorTick/iNumber*(24*3600));
LocalNowTime *time = [[LocalNowTime alloc]initWithUnixTime:unixTime];
text =[NSString stringWithFormat:@"%02d月%02d日",time.Month,time.Day];//@"%04d年%02d月%02d日"
}else{
//middle houre
text = [NSString stringWithFormat:@"%02d:00", iIndexMajorTick%24];
}
iIndexMajorTick++;
if((m_Style == DayStyle) &&(iIndexMajorTick> iDayCount+1)){
return;
}
point.y =self.frame.size.height*0.85;
[self drawLabelWithContext:context
atPoint:point
text:text
fillColor:labelFillColor
strokeColor: labelStrokeColor];
} else {
iIndexMinorTick++;
if (((iIndexMinorTick %10)==0) && (m_Style == MinuteStyle) ){
//seconds
NSString *text =[NSString stringWithFormat:@"%02d'", iIndexMinorTick*2];
point.y =self.frame.size.height*0.85;
[self drawLabelWithContext:context
atPoint:point
text:text
fillColor:minorTickColor
strokeColor: labelStrokeColor];
float fInc = minorTickLength/4.0;
point.y=self.frame.size.height*0.5-fInc;
[self drawMinorTickWithContext:context
atPoint:point
withColor: minorTickColor
width: minorTickWidth
length: minorTickLength+fInc*2];
}else{
point.y=self.frame.size.height*0.5;
[self drawMinorTickWithContext:context
atPoint:point
withColor: minorTickColor
width: minorTickWidth
length: minorTickLength];
}
}
}
- (BOOL)isMajorTick:(int)x {
int tick_number = x / minorTickDistance;
return (tick_number % minorTicksPerMajorTick) == 0;
}
- (void)setDialRangeFrom:(NSInteger)from to:(NSInteger)to {
// _minimum = from;
// _maximum = to;
// Resize the frame of the view
CGRect frame = self.frame;
// frame.size.width = (_maximum - _minimum) * _minorTickDistance + self.superview.frame.size.width;
NSLog(@"frame = %@", NSStringFromCGRect(frame));
self.frame = frame;
}
#pragma mark - Drawing
- (void)drawLabelWithContext:(CGContextRef)context
atPoint:(CGPoint)point
text:(NSString *)text
fillColor:(UIColor *)fillColor
strokeColor:(UIColor *)strokeColor {
CGSize boundingBox = [text sizeWithFont:labelFont];
NSInteger label_x = point.x - (boundingBox.width / 2);
if (label_x <=0) {
label_x =0;
}
CGContextSetStrokeColorWithColor(context, strokeColor.CGColor);
CGContextSetFillColorWithColor(context, fillColor.CGColor);
CGContextSetLineWidth(context, labelStrokeWidth);
CGRect rect;
if (m_Style == DayStyle) {
rect.origin.x =point.x;
}else{
rect.origin.x =label_x+1;
}
rect.origin.y =point.y+(self.frame.size.height-point.y -boundingBox.height)/2;
if (rect.origin.y <=0) {
rect.origin.y =self.frame.size.height-boundingBox.height;
}
rect.size.width = boundingBox.width;
rect.size.height = boundingBox.height;
// if (boundingBox.width <25) {
// rect =CGRectMake(label_x+boundingBox.width/2-8, self.frame.size.height-boundingBox.height, boundingBox.width, boundingBox.height);
// }else{
// rect =CGRectMake(label_x+boundingBox.width/2+1, self.frame.size.height-boundingBox.height, boundingBox.width, boundingBox.height);
// }
[text drawInRect:rect
withFont:labelFont
lineBreakMode:NSLineBreakByTruncatingTail
alignment:NSTextAlignmentCenter];
//CGContextSetShouldAntialias(context, NO );
}
- (void)drawMinorTickWithContext:(CGContextRef)context
atPoint:(CGPoint)point
withColor:(UIColor *)color
width:(CGFloat)width
length:(CGFloat)length {
CGContextSetStrokeColorWithColor(context, color.CGColor);
CGContextSetLineWidth(context, width);
CGContextMoveToPoint(context, point.x, point.y);
CGContextAddLineToPoint(context, point.x, point.y + length);
CGContextStrokePath(context);
}
- (void)drawMajorTickWithContext:(CGContextRef)context
atPoint:(CGPoint)point
withColor:(UIColor *)color
width:(CGFloat)width
length:(CGFloat)length {
// Draw the line
CGContextSetStrokeColorWithColor(context, color.CGColor);
CGContextSetLineWidth(context, width);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextMoveToPoint(context, point.x, point.y);
CGContextAddLineToPoint(context, point.x, point.y + length);
CGContextStrokePath(context);
}
-(UIImage *)getImageFromStyle:(TimeShiftedStyle)style{
m_Style = style;
//UIGraphicsBeginImageContext(imageView.frame.size);
UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, 0);
// Drawing code.
//获得处理的上下文
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, self.frame);
//设置线条样式
CGContextSetLineCap(context, kCGLineCapSquare);
//设置线条粗细宽度
CGContextSetLineWidth(context, 1.0);
//设置颜色
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 0.6);
//开始一个起始路径
CGContextBeginPath(context);
float fPointY =self.frame.size.height*0.34;
CGContextMoveToPoint(context, 0, fPointY);
CGContextAddLineToPoint(context, self.frame.size.width, fPointY);
fPointY =self.frame.size.height*0.813;
CGContextMoveToPoint(context, 0, fPointY);
CGContextAddLineToPoint(context, self.frame.size.width, fPointY);
CGContextStrokePath(context);
iIndexMajorTick =0;
fPointY =self.frame.size.height*0.39;
for (int i = 0; i < self.frame.size.width+1; i += minorTickDistance) {
CGPoint point = CGPointMake(i, fPointY);
[self drawTicksWithContext:context atPoint:point];
}
CGContextSetFillColorWithColor(context, HEX2RGB(@"#fafafa").CGColor);
CGRect blackRect = CGRectMake(self.frame.origin.x,
self.frame.size.height*0.53 ,
self.frame.size.width ,
self.frame.size.height*0.09);
CGContextFillRect(context, blackRect);
CGContextStrokePath(context);
CGContextSetLineCap(context, kCGLineCapSquare);
CGContextSetLineWidth(context, 1.0);
CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.6);
fPointY =self.frame.size.height*0.62;
CGContextMoveToPoint(context, 0, fPointY);
CGContextAddLineToPoint(context, self.frame.size.width, fPointY);
CGContextStrokePath(context);
image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// [labelStrokeColor release];
// [labelFillColor release];
// [labelFont release];
// [minorTickColor release];
// [majorTickColor release];
// [shadowColor release];
// labelStrokeColor=labelFillColor=labelFont=minorTickColor=majorTickColor=shadowColor=nil;
// NSLog(@"&&&1");
//
// //UIImage *img = [image copy];
// //UIImage *img
//UIImagePNGRepresentation(image);
// UIImage *img=[[UIImage alloc]initWithData:UIImagePNGRepresentation(image)];
// [image release];
// image =nil;
// NSLog(@"&&&2");
// UIImageWriteToSavedPhotosAlbum(image, nil,nil , nil ) ;
return image;
}
@end