3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CreateJSのTextクラスにて、テキストを自動折り返しする

Last updated at Posted at 2015-01-26

コード

/**
 * @description
 * `Text`クラスのインスタンス`textInstance`の`text`プロパティに、  
 * `textInstance`の幅をはみ出さないように、自動で折り返した`text`を格納する。
 * @param {Text} textInstance 自動で折り返した`text`を格納する`Text`クラスのインスタンス
 * @param {string} text `textInstance.text`に格納する自動で折り返す文字列
 */
function setWrapText(textInstance, text) {
  var initWidth = textInstance.lineWidth;
  var textArray = text.split('');
  var i = -1;
  var prevText = '';
  var lines = [];

  textInstance.text = '';

  while (textArray[++i]) {
    textInstance.text += textArray[i];

    if (textInstance.getMeasuredWidth() > initWidth) {
      lines.push(prevText);
      textInstance.text = textArray[i];
    }
    prevText = textInstance.text;
  }
  
  textInstance.text = lines.join('\n');
}

環境

EaselJS 0.7.1

参考

3
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?