Billede tekst
Hej eksperter,Er det muligt at få billeder til at stå på samme linje som tekst, uden at linjen bliver ombrudt?
Jeg har skrevet dette kode (i Flex). Det virker fint, men performance er helt i skoven.
<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%" creationComplete="init()">
<mx:Script>
<![CDATA[
import mx.core.UITextFormat;
import mx.core.UITextField;
import mx.controls.TextArea;
import mx.containers.Canvas;
import mx.controls.Image;
import mx.messaging.AbstractConsumer;
import mx.controls.Text;
import mx.controls.Label;
import mx.containers.BoxDirection;
private function init() : void
{
maxX = width - 20; // scrollbars
}
private function seperateSmilies(input:String) : String
{
var s:String = input;
for(var elm:Object in smilies)
{
var elmStr:String = elm as String;
var startIndex:Number = -1;
do
{
startIndex++;
var index:Number = s.indexOf(elmStr, startIndex);
if (index > -1)
{
var sLeft:String = s.substr(0, index);
var sRight:String = s.substr(index + elmStr.length);
s = sLeft;
if (sLeft.length > 0 && sLeft.charAt(sLeft.length-1) != " ")
{
s += " ";
}
s += elmStr;
if (sRight.length > 0 && sRight.charAt(0) != " ")
{
s += " ";
}
s += sRight;
}
startIndex = index;
} while (startIndex != -1)
}
return s;
}
public function AddText(text:String, fontSize:Number = 15, fontColor:uint = 0x000000, isBold:Boolean = false, fontFamily:String = "Arial") : void
{
var canvas:Canvas = new Canvas();
canvas.x = 0;
canvas.y = 0;
canvas.width = maxX;
canvas.setStyle("backgroundColor", 0xFF0000);
var words:Array = seperateSmilies(text).split(" ");
// add objects
for (var i:Number = 0; i < words.length; i++)
{
var w:String = words[i] as String;
// if empty
if (w == "")
{
continue;
}
// if smilie
if (smilies[w] != null)
{
var img:Image = new Image();
img.source = smilies[w];
canvas.addChild(img);
img.width = 24;
img.height = lineHeight;
}
// if text
else
{
var txt:Text = new Text();
txt.setStyle("fontSize", fontSize);
if (isBold)
{
txt.setStyle("fontWeight", "bold");
}
txt.setStyle("fontFamily", fontFamily);
txt.setStyle("color", fontColor);
txt.selectable = false;
txt.htmlText += w;
canvas.addChild(txt);
}
}
addChild(canvas);
messages.push(canvas);
PlaceObjects(canvas);
}
private function PlaceObjects(canvas:Canvas) : void
{
// place objects
var currentX:Number = 0;
var currentY:Number = 0;
validateNow();
for (var i:Number = 0; i < canvas.numChildren; i++)
{
var elm:DisplayObject = canvas.getChildAt(i);
var isImg:Boolean = elm is Image;
var elmWidth:Number = elm.width;
if (isImg)
{
elmWidth += 6;
}
if (currentX + elmWidth > maxX)
{
currentX = 0;
currentY += lineHeight;
}
if (isImg)
{
elm.x = currentX + 3;
}
else
{
elm.x = currentX;
}
if (isImg)
{
elm.y = currentY;
}
else
{
elm.y = currentY + correctTextY;
}
currentX += elmWidth;
}
}
public var smilies:Dictionary = new Dictionary();
public var maxMessages:Number = 50;
private var lineHeight:Number = 24;
private var correctTextY:Number = 6;
private var maxX:Number;
private var messages:Array = new Array();
private var numberContainer:Number = 0;
]]>
</mx:Script>
</mx:VBox>
Håber at der er nogen der har erfaring med dette.
PS. en løsning må også gerne bare være til Flash.
Martin.