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]];
}
運行結果: