j2me中 cavans 字符的自动换行函数

由于canvas中的drawstring 函数不能自动换行,因此在使用中必须考虑到换行问题

而换行很麻烦的是中英文混合,而且英文单词不能拆开显示。因此想了办法,可能在英文单词没有空格的时候处理还有些小问题,以后继续修改~~~~如有问题尽管提出

 

@string  : 源字符串

@font:      显示使用的字体

@FrameWidth : 屏幕宽度

 

    public Vector dealstrings( String string ,Font font , int FrameWidth ){
        int k = 0;
        Vector tempvec  = new Vector();
        String tempstring = string;
        int len = tempstring.length();
        for(  int j = 0 ,i = 0 ; i <= len ;i++ ){

            //处理到字符串结束:
             if(i >= tempstring.length())
              { 
                tempvec.addElement(tempstring.substring(j,i));
                break;
              }
          
             if (font.stringWidth(tempstring.substring(j,i)) <= FrameWidth && font.stringWidth(tempstring.substring(j,i+1)) > FrameWidth)
             {

                //当最后一个字符是中文,以及除空格以外的字符,符号时的处理
               if( ((int)tempstring.charAt(i-1) >= 19968 &&  (int)tempstring.charAt(i-1) <=  171941)  || 
                  (   (int)tempstring.charAt(i-1) >= 0 && (int)tempstring.charAt(i-1) <= 64  && (int)tempstring.charAt(i-1) != 32 )  ||
                     (   (int)tempstring.charAt(i-1) >= 91 && (int)tempstring.charAt(i-1) <= 96  )  ||
                     (   (int)tempstring.charAt(i-1) >= 123 && (int)tempstring.charAt(i-1) <= 127  ) 
                     ){
                         tempvec.addElement(tempstring.substring(j,i));
                         j=i;
                         continue;
                        }

                 //当最后一个字符或者下一个是空格
               else { k = i;
                     while(tempstring.charAt(i) != ' ' && tempstring.charAt(i-1) != ' ')
                          {   i--;
                                 if(i<=j){
                                    i= k ;
                                    break;
                                  }
                                 
                          }
                     if(tempstring.charAt(i) == ' ' ){
                     tempvec.addElement(tempstring.substring(j,i));
                     i++;
                     j=i;
                     continue;

                     }

                     else {
                     tempvec.addElement(tempstring.substring(j,i));
                     j=i;
                     continue;
                  
                     }
              
                  }
          
            }
         
        }
       
       return tempvec;
       
    }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值