2012年9月12日 星期三

繪圖基本實作(二)

使用自定的中文字型,來畫出中文字的實作

免費字型檔下載區
http://www.wazu.jp/gallery/Fonts_ChineseTraditional.html



1. 新開一個全新的專案

2. 為了繪圖加入一個UIView的Class檔



3. 加入中文字型檔


4. 註冊中文字型檔,需要編輯Plist檔,在Application requires iphone來增加一個選項


5. 增加一個Fonts provided by application
6. 在Fonts provided by application的item0裡面加上中文字型檔BiauKai.ttf (標楷體)

7. 在drawChinese.m加上繪圖所需的程式碼

#import "drawChinese.h"

@implementation drawChinese

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void) drawChineseText1:(NSString *)text x:(float)x y:(float)y {
   
    UIFont *font = [UIFont fontWithName:@"BiauKai" size:20];
    [text drawAtPoint:CGPointMake(x, y) withFont:font];
}

- (void) drawChineseText2:(NSString *)text x:(float)x y:(float)y {
   
    UIFont *font = [UIFont fontWithName:@"Arial" size:20];
   
    [text drawAtPoint:CGPointMake(x, y) withFont:font];
}


- (void)drawTestText:(CGContextRef) context
{
    CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);
    NSString *text = @"蘋";
    [self drawChineseText1:text x:50 y:100];
    text = @"果";
    [self drawChineseText1:text x:50 y:130];
   
    text = @"蘋";
    [self drawChineseText2:text x:100 y:100];
    text = @"果";
    [self drawChineseText2:text x:100 y:130];
   
    CGContextStrokePath(context);
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
   
    [self drawTestText:context];
       
}

8. 在showChineseViewController.h加入drawChinese.h

#import <UIKit/UIKit.h>
#import "drawChinese.h"

@interface showChineseViewController : UIViewController

@end






9. 在showChineseViewController.m加上drawChinese的instance
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   
    drawChinese *drawText = [[drawChinese alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:drawText];
   
    drawText.backgroundColor = [UIColor whiteColor];
   
}




10. 完成後顯示字型,左邊是標楷體,右邊是系統中文字

沒有留言: