IOS开发(98)之非XIB文件的自定义视图
1 前言
上一节我们谈到用XIB文件构建的自定义视图,今天我们来介绍一下,不借助XIB文件的自定义视图,共同学习一下。
2 详述
目录结构
这次我们不建立XIB文件,而是直接的Objective-C文件来代替XIB文件。
ZYCustomView.m:
[plain]
- (void)drawRect:(CGRect)rect
{
CGRect bounds = [self bounds];
CGPoint center;
center.x = bounds.origin.x + bounds.size.width / 2.0;
center.y = bounds.origin.y + bounds.size.height / 2.0;
//创建图形路径句柄
CGMutablePathRef path = CGPathCreateMutable();
//设置矩形的边界
CGRect rectangle = CGRectMake(center.x-100, center.y-150,200.0f, 300.0f);
//添加矩形到路径中
CGPathAddRect(path,NULL, rectangle);
//获得上下文句柄
CGContextRef currentContext = UIGraphicsGetCurrentContext();
//添加路径到上下文中
CGContextAddPath(currentContext, path);
//填充颜色
[[UIColor colorWithRed:0.20f green:0.60f blue:0.80f alpha:1.0f] setFill];
//设置画笔颜色
[[UIColor brownColor] setStroke];
//设置边框线条宽度
CGContextSetLineWidth(currentContext,5.0f);
//画图
CGContextDrawPath(currentContext, kCGPathFillStroke);
/* 释放路径 */
CGPathRelease(path);
// 创建一个字符串
NSString *text = @"我是Developer_Zhang";
UIFont *font = [UIFont boldSystemFontOfSize:20];
// 设置Rect
CGRect textRect;
textRect.size = [text sizeWithFont:font];
textRect.origin.x = center.x - textRect.size.width / 2.0;
textRect.origin.y = center.y - textRect.size.height / 2.0;
// 设置字体颜色
[[UIColor redColor] setFill];
//设置阴影
CGSize offset = CGSizeMake(4, 3);
//阴影颜色为黑色
CGColorRef color = [[UIColor blackColor] CGColor];
//模糊半径为2.0
CGContextSetShadowWithColor(currentContext, offset, 2.0, color);
[text drawInRect:textRect
withFont:font];
}
- (void)drawRect:(CGRect)rect
{
CGRect bounds = [self bounds];
CGPoint center;
center.x = bounds.origin.x + bounds.size.width / 2.0;
center.y = bounds.origin.y + bounds.size.height / 2.0;
//创建图形路径句柄
CGMutablePathRef path = CGPathCreateMutable();
//设置矩形的边界
CGRect rectangle = CGRectMake(center.x-100, center.y-150,200.0f, 300.0f);
//添加矩形到路径中
CGPathAddRect(path,NULL, rectangle);
//获得上下文句柄
CGContextRef currentContext = UIGraphicsGetCurrentContext();
//添加路径到上下文中
CGContextAddPath(currentContext, path);
//填充颜色
[[UIColor colorWithRed:0.20f green:0.60f blue:0.80f alpha:1.0f] setFill];
//设置画笔颜色
[[UIColor brownColor] setStroke];
//设置边框线条宽度
CGContextSetLineWidth(currentContext,5.0f);
//画图
CGContextDrawPath(currentContext, kCGPathFillStroke);
/* 释放路径 */
CGPathRelease(path);
// 创建一个字符串
NSString *text = @"我是Developer_Zhang";
UIFont *font = [UIFont boldSystemFontOfSize:20];
// 设置Rect
CGRect textRect;
textRect.size = [text sizeWithFont:font];
textRect.origin.x = center.x - textRect.size.width / 2.0;
textRect.origin.y = center.y - textRect.size.height / 2.0;
// 设置字体颜色
[[UIColor redColor] setFill];
//设置阴影
CGSize offset = CGSizeMake(4, 3);
//阴影颜色为黑色
CGColorRef color = [[UIColor blackColor] CGColor];
//模糊半径为2.0
CGContextSetShadowWithColor(currentContext, offset, 2.0, color);
[text drawInRect:textRect
withFont:font];
}
ZYViewController.m:
[plain]
- (void)viewDidLoad
{
[super viewDidLoad];
//创建一个窗体大小的CGRect
CGRect wholeWindow = [[self.view window] bounds];
// 创建一个窗体大小的HypnosisView实例
self.view = [[ZYCustomView alloc] initWithFrame:wholeWindow];
[self.view setBackgroundColor:[UIColor whiteColor]];
}
- (void)viewDidLoad
{
[super viewDidLoad];
//创建一个窗体大小的CGRect
CGRect wholeWindow = [[self.view window] bounds];
// 创建一个窗体大小的HypnosisView实例
self.view = [[ZYCustomView alloc] initWithFrame:wholeWindow];
[self.view setBackgroundColor:[UIColor whiteColor]];
}
运行结果:
补充:移动开发 , IOS ,