TextFormat & TextField

스크립팅을 하면서 가장 번거로운 부분은 텍스트필드를 수작업으로 작성해야 하는 경우입니다. 각각의 변수를 일일이 심볼로 만들어주고 그 안에 텍스트필드를 만들어줘야하는 경우가 대부분인데, 상당히 번거로운 일이죠.
때문에 텍스트 필드를 스크립트로 생성할 수 있다는 점은 대단히 매력적인 부분입니다.

scrInfoStyle = new TextFormat();
scrInfoStyle.size = 10;
scrInfoStyle.color = 0×333333;
scrInfoStyle.font = “Myriad Web”;
//텍스트 포맷의 선언
for (i=0; i _root.createEmptyMovieClip("scrInfo"+[i], 10+i);
_root["scrInfo"+i].createTextField(["Info"+i], i, 0, 0, 300, 50);
_root["scrInfo"+i]["Info"+i].text= screening[i].join(newline);
_root["scrInfo"+i]["Info"+i].selectable = false;
_root["scrInfo"+i]["Info"+i].embedFonts = true;
_root["scrInfo"+i]["Info"+i].setTextFormat(scrInfoStyle);
_root["scrInfo"+i]._x = 10;
_root["scrInfo"+i]._y = 40*i;
}

첫 부분의 코드는 텍스트포맷 객체를 선언한 코드입니다.
이처럼 한 개 이상의 텍스트포맷 객체를 선언한 이후 이를 텍스트 필드에 적용하는 것이 가능합니다. 이러한 스크립팅은 CSS와 같이 통합된 텍스트 스타일을 유지하는 것이 가능하도록 해줍니다. 주의할 점은 폰트 속성의 경우 반드시 심볼로서 무비에 해당하는 서체가 포함되어 있어야 합니다. 또한 이를 이용할 경우 textField.embedFonts = true; 와 같이 텍스트 필드의 임베드폰트 속성이 True 로 설정되어 있어야 합니다. 만약 이 속성이 False 로 되어있을 경우 Client 의 시스템 기본 서체로 맵핑(영문의 경우 Times New roman, 한글의 경우 굴림, 굴림체)이 됩니다. 이 속성이 False 인 상태에서 디폴트 폰트 이외의 폰트를 TextFormat.font의 속성에 지정할 경우 글씨가 전혀 나타나지 않습니다.(- -;) 자세한 사항은 아래의 O’Reily 의 액션스크립트 레퍼런스 가이드(Flash MX version 6 기준;2003년 현재에는 MX 2004 레퍼런스 가이드 해설집이 나오지 않은 것으로 압니다.)를 참고해야합니다.
다음 스크립트는 메인타임라인에 위치하여 무비의 좌표(0,0)에 300 x 50 크기의 텍스트필드를 생성합니다. 편의를 위해 for 문을 이용하여 무비클립과 그 안에 다시 텍스트 필드를 원하는 수만큼(여기서는 lastOrder+1만큼) 생성 가능하도록 한 코드입니다.
해당 스크립트를 적은 후 메인 타임라인에 onEnterFrame 이나 onChange 에 해당하는 콜백함수를 설정할 경우 변화하는 값을 그대로 텍스트필드에 반영하는 것이 가능합니다.


ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock

Chapter 18. ActionScript Language Reference
TextField.embedFonts Property Flash 6

——————————————————————————–
render text using fonts exported with the movie read/write
theField.embedFonts

Description
The Boolean embedFonts property specifies whether a field’s text is rendered using device fonts on the user’s system (false) or fonts embedded in the .swf file (true). When embedFonts is false, text appears without antialiasing and missing fonts are replaced by the system’s default font (usually some variation of Times New Roman). When embedFonts is true and the field’s fonts are exported with the .swf file, text appears antialiased. When embedFonts is true but one or more of the field’s fonts is missing from the .swf file, text in the missing fonts does not appear at all. Embedding a complete Roman font typically adds 20-30 KB to a movie (Asian fonts can be much larger). Using embedded fonts with sizes smaller than 10-point is not recommended, because antialiased text becomes unreadable below 10-point in most fonts.

The contents of a text field that is rotated or masked with an author-time mask layer will not show up on screen unless its font is embedded. However, as of Flash Player 6.0.40.0, MovieClip.setMask( ) can be used to apply a rectangular mask to a text field at runtime without embedded fonts. See MovieClip.setMask( ) for details.

To export (i.e., to embed) a font with a .swf file, we must do one of the following:

1. Create a new font symbol in the movie’s Library.
2. Manually add a dummy text field at authoring time and set its Character options in the Property inspector.

The font symbol approach always exports an entire font, while the text field approach allows us to export only part of a font, reducing file size. To export a subset of a font with a font symbol, we must use font-making software such as Macromedia Fontographer to create a custom font as a subset of the original font.

To export Arial Bold using a font symbol:

1. Select Window > Library.
2. From the pop-up Options menu in the upper-right corner of the panel, select New Font. The Font Symbol Properties dialog box appears.
3. Under Font, select Arial.
4. Under Style, select Bold.
5. Under Name, type ArialBold (this is a cosmetic name, used in the Library only).
6. In the Library, select the ArialBold font symbol.
7. From the pop-up Options menu, select Linkage.
8. In the Symbol Linkage Properties dialog box, under Linkage, select Export For ActionScript.
9. In the Identifier box, type ArialBold. This name will be used as the value of a TextFormat object’s font property.

To export any font using a dummy text field:

1. Select the Text tool.
2. Draw a rectangle on stage.
3. In the Property inspector, choose the desired font. The font’s actual name will be used as the value of a TextFormat object’s font property.
4. In the Property inspector, click the Character button and choose the character outlines to export.

Every variation of a font style must be embedded individually. If we use Courier New in bold, italic, and bold italic in a text field, then we must embed all three font variations or the text will not display correctly. Underline is not considered a font variation, nor is font size or color.

Setting embedFonts to true does not cause any fonts to download with the movie; it merely indicates that theField should be rendered with exported fonts if they are available. The embedFonts property must be set separately for each text field that uses a particular font, even if multiple text fields use the same font. However, file size is not affected when multiple text fields embed the same font only one copy of the font is downloaded with the movie.

To specify the font for a text field, use a TextFormat object and the TextField.setTextFormat( ) and TextField.setNewTextFormat( ) methods. To retrieve a list of fonts available on the user’s system, use TextField.getFontList( ).

On operating systems that support Unicode (e.g., Windows XP and Mac OS X), when embedFonts is false and a character is needed that is not available in the specified font, Flash will automatically search the system for a font that can represent the character. If a suitable font is found, it will be used until the next missing character is encountered, at which point Flash will again search for a suitable font.

Bugs
Theoretically, to apply the same embedFonts value to all text fields in a movie, we would use this syntax:

TextField.prototype.embedFonts = true;
However, in Flash 6, built-in properties set on TextField.prototype are not inherited by text field instances.

Example
Assuming the Verdana font is exported, the following code causes theField_txt to be rendered in antialiased Verdana:

// Make the text field
this.createTextField(“theField_txt”, 1, 0, 0, 150, 40);
// Tell the field to use embedded fonts
theField_txt.embedFonts = true;
// Assign some text to the field
theField_txt.text = “How are you?”
// Create and apply a text format with a font of Verdana
defaultFormat = new TextFormat();
defaultFormat.font = “Verdana”;
theField_txt.setTextFormat(defaultFormat);

See Also
TextField.getFontList( ), TextField.setNewTextFormat( ), TextField.setTextFormat( ), TextFormat.font


About this entry