Flash&ActionScript/ActionScript
2008/10/24 15:09
TextLineMetrics 사용하기
var mc:Sprite = new Sprite();
this.addChild(mc);
var format:TextFormat = new TextFormat();
format.size = 100;
//tf (폰트 정해놓은 텍스트 필드)
tf.autoSize = TextFieldAutoSize.LEFT;
tf.multiline = true;
//텍스트 포맷 먼저 설정
tf.defaultTextFormat = format;
//tf.setTextFormat(format);
// 텍스트 포맷 설정 후 텍스트 입력
tf.text = "萬\n病痛治\n藥";
trace("tf.numLines " + tf.numLines);
trace("tf.getLineLength(tf.numLines - 1) " + tf.getLineLength(tf.numLines - 1));
trace("tf.getLineText(tf.numLines - 1) " + tf.getLineText(tf.numLines - 1));
var str:String = tf.getLineText(tf.numLines - 1);
trace("str.length /" + str.length);
trace("str / " +str);
mc.graphics.beginFill(0,0);
mc.graphics.lineStyle(1,0);
mc.graphics.drawRect(tf.x,tf.y,str.length * (format.size+15) - (15), tf.textHeight);
mc.graphics.endFill();
//텍스트 라인 메트릭스로 아주 정확한 범위를 구할 수 있다.
var tfMetrics:TextLineMetrics = tf.getLineMetrics(tf.numLines - 1);
mc.graphics.beginFill(0,0);
mc.graphics.lineStyle(1,0xff0000);
mc.graphics.drawRect(tf.x,tf.y,tfMetrics.width, tf.textHeight);
mc.graphics.endFill();

