Flash AS提供这样一个类:Selection类,通过它的两个方法(getBeginIndex(),getEndIndex()),我们可以获取我们想要的文本的起始位置!使用方法如下:
startIndex = Selection.getBeginIndex();//选择文本的起点 
endIndex = Selection.getEndIndex();//选择文本的终点
获得起始位置之后,再使用setTextFormat()方法将样式附加到指定文本。
 
var Style = new TextFormat();//定义一个文本格式 
var startIndex:Number, endIndex:Number;//选择字符的起始位置 
style_txt.borderColor = 0xdddddd;//style_txt文本框的边框样式 
size_txt.borderColor = 0xdddddd;//size_txt文本框的边框样式 
style_txt.htmlText = "<a href=’http://www.blue-sun.cn’>蓝光博客 BlueShine</a>" 
+"<br>http://www.blue-sun.cn<br>当你选择部分文本后,修改下列样式,"+ 
"文本框中被选择的文本样式是不是发生了变化!?"; 
// 
this.onEnterFrame = function() { 
    if (Selection.getFocus() == String(style_txt)) {//判断文本框是否获得焦点 
        startIndex = Selection.getBeginIndex();//选择文本的起点 
        endIndex = Selection.getEndIndex();//选择文本的终点 
    } 
}; 
//修改字体大小 
size_txt.onChanged = function() { 
    Style=style_txt.getTextFormat(startIndex, endIndex);//获得改变之前的文本样式 
    Style.size = this.text;//设置属性值 
    _root.style_txt.setTextFormat(startIndex, endIndex, Style);//将样式附加到文本 
}; 
//修改文本颜色 
var listenOBJ_Com = new Object(); 
listenOBJ_Com.change = function() { 
    Style=style_txt.getTextFormat(startIndex, endIndex); 
    Style.color = color_Com.text; 
    _root.style_txt.setTextFormat(startIndex, endIndex, Style); 
}; 
color_Com.addEventListener("change", listenOBJ_Com); 
//修改字体粗细 
b_bt.onRelease = function() { 
    Style=style_txt.getTextFormat(startIndex, endIndex); 
    Style.bold = !Style.bold; 
    _root.style_txt.setTextFormat(startIndex, endIndex, Style);     
} 
//切换斜体 
i_bt.onRelease = function() { 
    Style=style_txt.getTextFormat(startIndex, endIndex); 
    Style.italic = !Style.italic; 
    _root.style_txt.setTextFormat(startIndex, endIndex, Style);     
} 
//文本左对齐 
l_bt.onRelease = function() { 
    Style=style_txt.getTextFormat(startIndex, endIndex); 
    Style.align ="left"; 
    _root.style_txt.setTextFormat(startIndex, endIndex, Style);     
} 
//文本居中 
c_bt.onRelease = function() { #p#分页标题#e#
    Style=style_txt.getTextFormat(startIndex, endIndex); 
    Style.align ="center"; 
    _root.style_txt.setTextFormat(startIndex, endIndex, Style);     
} 
//文本右对齐 
r_bt.onRelease = function() { 
    Style=style_txt.getTextFormat(startIndex, endIndex); 
    Style.align ="right"; 
    _root.style_txt.setTextFormat(startIndex, endIndex, Style);     
}
说明:Selection.getFocus() 的作用是得到获得焦点的文本框名称!从而可以据此判断文本框是否获得焦点