Flash文本框局部文本样式的控制

资讯 驱动中国 xuqingzhong 2,949 次阅读 0 条评论
分享 Q

 

 

先看看效果:

Flash AS提供这样一个类:Selection类,通过它的两个方法(getBeginIndex(),getEndIndex()),我们可以获取我们想要的文本的起始位置!使用方法如下:
startIndex = Selection.getBeginIndex();//选择文本的起点
endIndex = Selection.getEndIndex();//选择文本的终点

获得起始位置之后,再使用setTextFormat()方法将样式附加到指定文本。
_root.style_txt.setTextFormat(startIndex, endIndex, Style)

实例详细代码如下:
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() 的作用是得到获得焦点的文本框名称!从而可以据此判断文本框是否获得焦点
版权声明:本文由驱动中国整理发布,转载需注明出处与原文链接。如有疑问请联系 editor@qudong.com
本文价值 成为第一个评分的人
登录后为本文打分
网友评论0 条评论
正在回复 的评论取消
评论加载中…
🔐

登录后参与互动

评论、点赞均可赚积分
连续签到、邀请好友也有奖励 🎁

电脑端: 微信扫码登录 微信内: 一键登录

微信扫一扫

打开微信「扫一扫」,分享本文到朋友圈或好友