IOS開發(98)之非XIB檔案的自訂視圖

來源:互聯網
上載者:User

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

 



 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.