zoukankan      html  css  js  c++  java
  • [ActionScript 3.0] 用TextField的方法getCharIndexAtPoint(x:Number, y:Number):int实现文字在固定范围内显示

    有时候我们遇到一行文字过多时必须固定文字的显示范围,但由于中英文所占字节数不一样,所以不能很好的用截取字符的方式去统一显示范围的大小,用TextField的getCharIndexAtPoint(x:Number, y:Number):int方法可以方便的实现:

    getCharIndexAtPoint(x:Number, y:Number):int   在 x 和 y 参数指定的位置返回从零开始的字符索引值。演示一个小例子:

    import flash.text.TextField;
    import flash.geom.Point;
    import flash.text.TextFieldAutoSize;
    var index:int;
    var str:String = "没有把握的事,不要抱希望,那就不会失望。无法揣摩那个人,那就不要请求他替你做些什么事情,不让他有机会拒绝你,你才不会失望。不要爱上一个看来不会爱上你的人,那就不用失望。";
    var _charPoint:Point = new Point(200,5);
    var txt:TextField = new TextField  ;
    txt.autoSize = TextFieldAutoSize.LEFT;
    this.addChild(txt);
    txt.text = str;
    
    trace(txt.width);
    
    index = txt.getCharIndexAtPoint(_charPoint.x,_charPoint.y);
    if ((index != -1))
    {
        txt.text = str.substr(0,index) + "…";
    }
    else
    {
        txt.text = str;
    }
  • 相关阅读:
    关于typecho发布文章后的错位
    Python3 和 Python2的区别
    django apache error.log过大
    php 数组转json格式
    php get传递数据
    SVN中文件属性
    linux中django+apache配置
    php添加环境变量
    php和apache安装心得
    php 5.6.14手动安装 php -v 显示没有安装
  • 原文地址:https://www.cnblogs.com/frost-yen/p/4519581.html
Copyright © 2011-2022 走看看