文章目录
  1. 1. 下载字体
  2. 2. 安装字体到Mac
  3. 3. 打开字形
  4. 4. Xcode当前设置
    1. 4.1. 添加到project中
    2. 4.2. 设置info.plist文件
    3. 4.3. 确保添加到资源文件中
  5. 5. 设置
    1. 5.1. 关键代码
    2. 5.2. 文字生成图片
  6. 6. 参考

20161229148301025045015.jpg

下载字体

字体格式为’ttf’

20161229148301043375443.jpg
下载地址1
下载地址2

安装字体到Mac

双击即可

20161229148301048017773.jpg

打开字形

20161229148301055239441.jpg

Xcode当前设置

添加到project

20161229148301086740078.jpg

设置info.plist文件

20161229148301092779535.jpg

确保添加到资源文件中

20161229148301102120785.jpg

设置

关键代码

1
2
3
CFStringRef fontName =CFSTR("HanWangWCL10");
CTFontRef font =
CTFontCreateWithName(fontName, 30, NULL);

文字生成图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
UIImage * GetImage( NSString * str) {
CGSize Size = CGSizeMake(60, 60);
UIGraphicsBeginImageContextWithOptions(Size,NO,0.0);
double width; CGContextRef context; CGPoint textPosition; CFAttributedStringRef attrString;
// Initialize those variables.
const char * N = [str UTF8String];
CFStringRef string = CFStringCreateWithCString(kCFAllocatorDefault, N, kCFStringEncodingUTF8 );

//只绘制150字节 的字符串
width =60;
context = UIGraphicsGetCurrentContext();

CGContextTranslateCTM(context, 0, Size.height);

CGContextScaleCTM(context, 1.0, -1.0);

//起始位置
textPosition = CGPointMake(0.0, 15.0);
//字形
//CFStringRef fontName =CFSTR(/*"Papyrus"*/"HanziPenSC-W3");
//iosfonts.com
/**
* HanWangFangSongMedium
*
* HanWangShinSuMedium
*
* HanWangWCL10
*/
CFStringRef fontName =CFSTR("HanWangWCL10");
CTFontRef font =
CTFontCreateWithName(fontName, 30, NULL);
//笔调重描痕迹
CGFloat number = 3.0;

CFNumberRef strokeWidth = CFNumberCreate(kCFAllocatorDefault, kCFNumberFloatType, &number );
//笔调颜色重描痕迹
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {0.5,0.5,0.6,1};
CGColorRef color = CGColorCreate(space, components);
// Controls vertical text positioning
CGFloat superScriptnumber = 1;

CFNumberRef superScript = CFNumberCreate(kCFAllocatorDefault, kCFNumberCGFloatType, &superScriptnumber );
//kCTUnderlineStyleAttributeName
/**
* kCTUnderlineStyleNone = 0x00,
kCTUnderlineStyleSingle = 0x01,
kCTUnderlineStyleThick = 0x02,
kCTUnderlineStyleDouble = 0x09
*/
CGFloat UnderlineStylenumber =kCTUnderlineStyleNone|kCTUnderlinePatternSolid ;

CFNumberRef UnderlineStyle = CFNumberCreate(kCFAllocatorDefault, kCFNumberCGFloatType, &UnderlineStylenumber );
//kCTUnderlineColorAttributeName
CGColorSpaceRef Underlinespace = CGColorSpaceCreateDeviceRGB();
CGFloat Underlinecomponents[] = {0.0,0.5,0.5,1};
CGColorRef Underlinecolor = CGColorCreate(Underlinespace, Underlinecomponents);
//kCTVerticalFormsAttributeName 文字方向
CFBooleanRef t = kCFBooleanFalse;


CFStringRef keys[] = { kCTFontAttributeName,kCTStrokeWidthAttributeName ,kCTStrokeColorAttributeName,kCTSuperscriptAttributeName,kCTUnderlineColorAttributeName,kCTUnderlineStyleAttributeName,kCTVerticalFormsAttributeName/*,kCTGlyphInfoAttributeName*/ };

CFTypeRef values[] = { font ,strokeWidth,color,superScript,Underlinecolor, UnderlineStyle ,t/*,info*/};



CFDictionaryRef attributes =

CFDictionaryCreate(kCFAllocatorDefault,(const void**)&keys,

(const void**)&values,sizeof(keys)/sizeof(keys[0]) ,

&kCFTypeDictionaryKeyCallBacks,

&kCFTypeDictionaryValueCallBacks);



attrString = CFAttributedStringCreate(kCFAllocatorDefault, string, attributes);

// Create a typesetter using the attributed string.
CTTypesetterRef typesetter = CTTypesetterCreateWithAttributedString(attrString);

// Find a break for line from the beginning of the string to the given width.
CFIndex start = 0;
CFIndex count = CTTypesetterSuggestLineBreak(typesetter, start, width);

// Use the returned character count (to the break) to create the line.
CTLineRef line = CTTypesetterCreateLine(typesetter, CFRangeMake(start, count));

// Get the offset needed to center the line.
float flush = 0.5; // centered
double penOffset = CTLineGetPenOffsetForFlush(line, flush, width);

// Move the given text drawing position by the calculated offset and draw the line.
CGContextSetTextPosition(context, textPosition.x + penOffset, textPosition.y);
CTLineDraw(line, context);

// Move the index beyond the line break.
start += count;
CGColorRelease(color);
CGColorRelease(Underlinecolor);
CGColorSpaceRelease(Underlinespace);
CGColorSpaceRelease(space);
// transfer image
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}

参考

文章目录
  1. 1. 下载字体
  2. 2. 安装字体到Mac
  3. 3. 打开字形
  4. 4. Xcode当前设置
    1. 4.1. 添加到project中
    2. 4.2. 设置info.plist文件
    3. 4.3. 确保添加到资源文件中
  5. 5. 设置
    1. 5.1. 关键代码
    2. 5.2. 文字生成图片
  6. 6. 参考