引き続きFlash テキストレイアウトフレームワークの使い方を調べています。
前回はCore ComponentとConversion Componentを扱いました。
今回は3つめのEdit Componentを使っていきたいと思います。
・The Text Layout Edit Component (textLayout_edit.swc)
Edit ComponentにはSelectionManager、EditManager、UndoManagerという3つの管理クラスがあります。
使い方は簡単なので、この3つのクラスをまとめて使った例を載せます。
・テキストの選択・変更・アンドゥ/リドゥを可能にする
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import flashx.textLayout.container.DisplayObjectContainerController; import flashx.textLayout.conversion.TextFilter; import flashx.textLayout.edit.EditManager; import flashx.textLayout.edit.SelectionManager; import flashx.textLayout.edit.UndoManager; import flashx.textLayout.elements.*; var text:String = "<TextFlow xmlns='http://ns.adobe.com/textLayout/2008' fontFamily='Helvetica' fontSize='25' color='#666666' textIndent='15' marginBottom='15' paddingTop='4' paddingLeft='4'><p><span>There are many </span><span fontStyle='italic'>such</span><span> lime-kilns in that tract of country, for the purpose of burning the white marble which composes a large part of the substance of the hills. Some of them, built years ago, and long deserted, with weeds growing in the vacant round of the interior, which is open to the sky, and grass and wild-flowers rooting themselves into the chinks of the stones, look already like relics of antiquity, and may yet be overspread with the lichens of centuries to come. Others, where the lime-burner still feeds his daily and nightlong fire, afford points of interest to the wanderer among the hills, who seats himself on a log of wood or a fragment of marble, to hold a chat with the solitary man. It is a lonesome, and, when the character is inclined to thought, may be an intensely thoughtful occupation; as it proved in the case of Ethan Brand, who had mused to such strange purpose, in days gone by, while the fire in this very kiln was burning.</span></p><p><span>The man who now watched the fire was of a different order, and troubled himself with no thoughts save the very few that were requisite to his business. At frequent intervals, he flung back the clashing weight of the iron door, and, turning his face from the insufferable glare, thrust in huge logs of oak, or stirred the immense brands with a long pole. Within the furnace were seen the curling and riotous flames, and the burning marble, almost molten with the intensity of heat; while without, the reflection of the fire quivered on the dark intricacy of the surrounding forest, and showed in the foreground a bright and ruddy little picture of the hut, the spring beside its door, the athletic and coal-begrimed figure of the lime-burner, and the half-frightened child, shrinking into the protection of his father's shadow. And when again the iron door was closed, then reappeared the tender light of the half-full moon, which vainly strove to trace out the indistinct shapes of the neighboring mountains; and, in the upper sky, there was a flitting congregation of clouds, still faintly tinged with the rosy sunset, though thus far down into the valley the sunshine had vanished long and long ago.</span></p></TextFlow>"; var flow:TextFlow = TextFilter.importToFlow(text, TextFilter.TEXT_LAYOUT_FORMAT); var undoMgr:UndoManager = new UndoManager(); flow.interactionManager = new SelectionManager(); flow.interactionManager = new EditManager(undoMgr); var textSprite:Sprite = new Sprite(); flow.flowComposer.addController(new DisplayObjectContainerController(textSprite, 400, 200)); flow.flowComposer.updateAllContainers(); |
(Flashが表示されない場合はこちら→ EditMgr.swf)
これでアンドゥ/リドゥ機能が付いたテキストエリアが作成されました。
テキスト入力してからCtrl + Z、Ctrl + Y等で動作を確認してみてください。
また、テキストを選択して下にドラッグするとエリアがスクロールします(カーソルキー↓でもOK)。
基本的にはTextFlow.interactionManagerプロパティにインスタンスを関連付けるだけです。
入力履歴のスタックサイズはUndoManager.undoAndRedoItemLimitプロパティで変更可能です。
ちなみにこのプロパティの初期値を調べてみると25になっていました。
もしテキストエリアをスクロールさせたくなければ、DisplayObjectContainerController.verticalScrollPolicy (横方向ならhorizontalScrollPolicy)プロパティを ScrollPolicy.OFFと指定します。
EditManagerについては資料にもっといろいろ書いてあったんですが、まだよく読めていません。
今後、もっと高度な処理が必要になった時に改めて;
あとは、テキストのフォーマット(カラーやフォントスタイル等)やレイアウト周り(マージンやインデント等)の設定方法などを追って調べていきたいと思います。
・関連記事
Flash Text Layout Framework
One thought