TextField 小結

對於textfield中文本設置不同字體:

textfield可以對選中文本設置字體, 當一段文本有兩種文本格式(即textformat)時, 可以獲取光標所在位置前一個字符的textformat, 這樣就可以取到不同樣式文本的不同textformat, 而不影響其整體.但當修改整體文本格式時, 原有被修改的文本格式將丟失, 使用新定義的整體文本格式.

注:此處附帶一小例子, 見本文最下方附件例一!

-------------------------------------------------------------------

對於textfield中文字設置兩種顏色, 佈局後變成一種顏色的修改:

new一個textformat賦值給當前textfield, textformat屬性可以疊加, 而不要修改原有textformat的align屬性.(同理可應用於其他屬性)

-------------------------------------------------------------------

訪問剪貼板:

System.setClipboard("文本內容");//將文本內容放入剪貼板
Clipboard.generalClipboard.setData(ClipboardFormats.TEXT_FORMAT, "文本內容");//將文本內容放入剪貼板
var t:String = Clipboard.generalClipboard.getData(ClipboardFormats.TEXT_FORMAT) as String;//讀取剪貼板中內容
    注: 此處訪問剪貼板需用戶執行操作, 可以是按鈕點擊或鍵盤操作

-----------------------------------------------------------------
獲取計算機安裝字體:

var arr:Array = Font.enumerateFonts(true); 
arr.sortOn("fontName", Array.CASEINSENSITIVE);

------------------------------------------------------------------

TextField文本對齊:

1. 使用TextFormat的align屬性

    textFormat.align = TextFormatAlign.CENTER;

    但是這個屬性在myEclipse中給出的提示是指定的 align 不是 flash.text.TextFormatAlign 的成員, 用上面的方法沒有效果. 

    所以就直接寫 textFormat.align = "center"; 這樣就好用了. 

2. 使用TextField的autoSize屬性

    textField.autoSize = TextFieldAutoSize.RIGHT;

    但是這個屬性在myEclipse中給出的提示是指定的 autoSize 不是 flash.text.TextFieldAutoSize 的成員, 用這個的方法也沒有效果. 

    有人說是因爲設置了TextField的wordWrap屬性, 對autoSize產生了影響, 可是去掉了TextField屬性, 也沒有效果. (API沒解釋)

    注:textformat的align可實現兩端對齊兩端對齊, textfield的autoSize不能

---------------------------------------------------------------

TextField控制可編輯:

txttalk.type=TextFieldType.DYNAMIC;//不可編輯
txttalk.type=TextFieldType.INPUT;//可編輯

--------------------------------------------------------------

TextField 設置可輸入中文:

先獲取焦點, 再修改IME:

input.addEventListener(FocusEvent.FOCUS_IN, focusInHandler);
function focusInHandler(e:FocusEvent):void
{
    IME.enabled = true;
}

--------------------------------------------------------------

textfield添加項目符號:

textformat.bullet = true;

---------------------------------------------------------------

textfield替換文本的方式:

textfield.text  ---> String

textfield.htmlText  ---> HTML格式文本

textfield.replaceSelectedText(String)  ---> 可用textfield.setSelection(beginIndex, endIndex)方法設置選中位置

textfield.replaceText(beginIndex, endIndex, String)

---------------------------------------------------------------

TextField 對文字編輯的監聽:

這個事件可監聽文字的增刪改, 均可觸發:

textField.addEventListener(Event.CHANGE, textChangeHandler);
網上好多都說用這個事件, 但是我加上之後, 這個事件只能監聽文本添加, 對於刪除不觸發監聽, 有待學習.
textField.addEventListener(TextEvent.TEXT_INPUT, textInputHandler);

  查看了TextArea的源碼, 發現裏面有個這個, 但是放到我的代碼裏沒好用, 沒找到原因爲何, 在此先做記錄.

 [Bindable("textChanged")]
    
    // Compiler will strip leading and trailing whitespace from text string.
    [CollapseWhiteSpace]
       
    /**
     *  @private
     */
    override public function set text(value:String):void
    {
        // Of 'text', 'textFlow', and 'content', the last one set wins.
        
        super.text = value;
        
        // Trigger bindings to textChanged.
        dispatchEvent(new Event("textChanged"));        
    }


附件附件附件附件附件附件附件:

例一:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
					   xmlns:s="library://ns.adobe.com/flex/spark" 
					   xmlns:mx="library://ns.adobe.com/flex/mx">
	<s:layout>
		<s:VerticalLayout/>
	</s:layout>

	<fx:Script>
		<![CDATA[
			import mx.controls.Alert;
			import mx.core.UIComponent;
			
			private var tf:TextField = new TextField();
			
			protected function button1_clickHandler(event:MouseEvent):void
			{
				tf.x = tf.y = 100;
				tf.width = 400;
				tf.height = 300;
				tf.border = true;
				tf.borderColor = 0x0000ff;
				tf.text = "好好學習 天天向上";
				tf.type = TextFieldType.INPUT;
				
				var tfor1:TextFormat = new TextFormat();
				tfor1.color = 0x00ffff;
				tfor1.size = 20;
				tfor1.font = "STCaiyun";//華文彩雲
				tf.setTextFormat(tfor1, 0, 5);
				
				var tfor2:TextFormat = new TextFormat();
				tfor2.color = 0xff00ff;
				tfor2.size = 20;
				tfor2.font = "STXingkai";//華文行楷
				tf.setTextFormat(tfor2, 5, tf.length);
				
				tf.addEventListener(MouseEvent.CLICK, textClickHandler);
				
				var u:UIComponent = new UIComponent();
				u.addChild(tf);
				bc.addElement(u);
			}
			
			private function textClickHandler(e:MouseEvent):void
			{
				var t:TextFormat = null;
				if(tf.selectionBeginIndex == 0)
				{
					t = tf.getTextFormat(tf.selectionBeginIndex, tf.selectionBeginIndex + 1);
				}
				else
				{
					t = tf.getTextFormat(tf.selectionBeginIndex - 1, tf.selectionBeginIndex);
				}
				Alert.show(t.font);
			}
		]]>
	</fx:Script>

	<fx:Declarations>
		<!-- 將非可視元素(例如服務、值對象)放在此處 -->
	</fx:Declarations>
	<s:Button label="ClickMe" click="button1_clickHandler(event)"/>
	<s:BorderContainer id="bc" width="100%" height="100%"/>
</s:WindowedApplication>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章