今天群里的迷同志问我怎么用AS嵌入字体,很久以前的东西了,现在不怎么记得了。
还是重新做一遍温习温习,然后也放这里来以免以后全忘掉了,温习的机会都没了。
首先在新建个fla命名为font.fla, Ctrl+L
调出库面板。
右键点击库面板的空白处,新建字体.... 调出 字体元件属性 窗口。
选择要嵌入的字体, 并选择为ActionScript 导出. 填入自己的类名。
确定并Ctrl+Enter
导出 font.swf。
OK,字体库已经准备就绪了,下面是应用篇。
下面是应用篇的为文档类。
提示:
- Font.registerFont(MyFont); 注册全局字体
- new TextFormat(myFont.fontName); 应用字体
- txt.embedFonts = true; 设置嵌入字体
以上3条必须同时用到才OK的。
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
|
package
{
import
flash.display
.
Loader
;
import
flash.display
.
Sprite
;
import
flash.events
.
Event
;
import
flash.events
.
ProgressEvent
;
import
flash
.
net
.
URLRequest
;
import
flash.system
.
LoaderContext
;
import
flash.text
.
Font
;
import
flash.text
.
TextField
;
import
flash.text
.
TextFormat
;
import
flash.utils
.
getDefinitionByName
;
/**
* www.litefeel.com
* @author lite3
*/
[
SWF
(
width
=
600
,
height
=
50
)
]
public
class
EmbedFontDemo
extends
Sprite
{
private
var
txt
:
TextField
;
private
var
fontLoader
:
Loader
;
public
function
EmbedFontDemo
(
)
:
void
{
txt
=
new
TextField
(
)
;
txt
.
x
=
50
;
txt
.
y
=
10
;
txt
.
width
=
500
;
txt
.
height
=
30
;
txt
.
border
=
true
;
txt
.
textColor
=
0x0099FF
;
addChild
(
txt
)
;
fontLoader
=
new
Loader
(
)
;
fontLoader
.
load
(
new
URLRequest
(
"http://litefeel.com/assets/swf/embedFontDemo/font.swf"
)
,
new
LoaderContext
(
false
,
loaderInfo
.
applicationDomain
)
)
;
fontLoader
.
contentLoaderInfo
.
addEventListener
(
Event
.
COMPLETE
,
completeHandler
)
;
fontLoader
.
contentLoaderInfo
.
addEventListener
(
ProgressEvent
.
PROGRESS
,
progressHandler
)
;
}
private
function
progressHandler
(
e
:
ProgressEvent
)
:
void
{
var
ratio
:
int
=
e
.
bytesLoaded
/
e
.
bytesTotal
*
100
;
txt
.
text
=
"loading... "
+
ratio
+
"%"
;
}
private
function
completeHandler
(
e
:
Event
)
:
void
{
fontLoader
.
contentLoaderInfo
.
removeEventListener
(
Event
.
COMPLETE
,
completeHandler
)
;
fontLoader
.
contentLoaderInfo
.
removeEventListener
(
ProgressEvent
.
PROGRESS
,
progressHandler
)
;
trace
(
"font coomplete!"
)
;
var
MyFont
:
Class
=
getDefinitionByName
(
"cn.lite3.font.Font_hyqytj"
)
as
Class
;
// 注册全局字体
Font
.
registerFont
(
MyFont
)
;
var
myFont
:
Font
=
new
MyFont
(
)
as
Font
;
// 应用字体
var
format
:
TextFormat
=
new
TextFormat
(
myFont
.
fontName
,
25
,
null
,
true
)
;
txt
.
defaultTextFormat
=
format
;
// 嵌入字体
txt
.
embedFonts
=
true
;
txt
.
text
=
"lite3 欢迎大家访问我的博客 www.litefeel.com"
;
}
}
}
|
源码下载☞
font.swf 下载地址:http://www.litefeel.com/assets/swf/embedFontDemo/font.swf