无法访问 htmlText 中的嵌入图像
可以使用 htmlText
属性将图像包含在 TextArea
控件中:
ta.htmlText = '<img src="http://..."/>';
如何引用嵌入图像?
一个例子:
<mx:TextArea xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
[Embed(source='../assets/img.gif')]
public var img:Class;
]]>
</mx:Script>
<mx:htmlText>
<![CDATA[
<img src="???" />
]]>
</mx:htmlText>
</mx:TextArea>
UPD:
<img src='../assets/img.gif />
在本地计算机上工作,但在服务器环境中它会抛出:
Error #2044: Unhandled IOErrorEvent:. text=Error #2035: URL Not Found.
我该如何解决这个问题?
Images can be included in TextArea
controls using the htmlText
property:
ta.htmlText = '<img src="http://..."/>';
How can I reference embedded images?
An example:
<mx:TextArea xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
[Embed(source='../assets/img.gif')]
public var img:Class;
]]>
</mx:Script>
<mx:htmlText>
<![CDATA[
<img src="???" />
]]>
</mx:htmlText>
</mx:TextArea>
UPD:
<img src='../assets/img.gif />
works on local machine, but in server environment it throws:
Error #2044: Unhandled IOErrorEvent:. text=Error #2035: URL Not Found.
How can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
好的,我刚刚花了几个小时解决这个问题,但我已经让它在 Flash 和 Flex 中工作了。
在 TextField 中显示图像
您可以将 DisplayObjects 加载到 TextField
标记中,包括 MovieClip、Sprite 和嵌入图像。
在 Flash 或 Flex 中,如果您想显示嵌入图像,则必须创建一个扩展
DisplayObject
类(例如 Sprite 或 MovieClip)的包装类。确保您的文本字段足够大以包含图像。 在测试过程中,当我的 TextField 确实太小而无法显示整个图像时,我认为事情不起作用。
Flash 示例
简单的示例,所有代码都在主时间轴上。
在舞台上创建一个动态文本字段。 给它一个有用的名称,我称之为
txtImageTest
。将
txtImageTest
调整为合适的尺寸,例如300x150px。创建一个新的 MovieClip 元件并为其指定一个类名称,例如
imageClip1
。创建
在新剪辑中绘制某些内容或在
imageClip1
中放置嵌入图像。返回主时间线,取消选择所有对象并在第一帧上打开 Actionscript 编辑器。
返回主时间线,取消
在文本字段上启用多行和自动换行:
保存并测试您的电影。
Flex 示例
由于我们不能像在 Flash 中那样在库中创建新的 MovieClip,因此我们需要创建一个执行相同任务的新类(如果您想在 Flash 中创建新剪辑,则此方法也适用于 Flash)图书馆)。
这是我的黑色三角形项目符号类,称为 BlackArrow.as:
注意:不要不要扩展 Bitmap(在 Flash 中)或 BitmapAsset(在 Flex 中),因为它们无法缩放或在文本字段中正确定位。 选择 Sprite 或类似的东西。
下面是在 TextField 中显示此图像的类的示例:
请注意,我在图像标记的 src 属性中使用完全限定的类名。
最后注释
图像与文本字段的左侧或右侧对齐,具体取决于图像标记的
align
属性。 这意味着您无法在不直接访问图像的情况下将图像放置在文本中间。图像标签支持的属性列表如下:
http://blog.coursevector.com/notes-htmltext
TextField LiveDoc 页面是这里:
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3 /flash/text/TextField.html
您需要使用
getImageReference()
函数来获取文本字段中图像的引用:http://livedocs.adobe.com /flash/9.0/ActionScriptLangRefV3/flash/text/TextField.html#getImageReference%28%29
确保在
src
属性中使用完全限定的类名。OK, I've just spent a couple of hours sorting this out, but I've got it working in Flash and Flex.
Displaying images in a TextField
You can load DisplayObjects into TextField
<img />
tags including MovieClips, Sprites and embedded images.In Flash or Flex if you want to display an embedded image then you will have to create a wrapper class that extends a
DisplayObject
class (e.g. Sprite or MovieClip).Make sure your text field is large enough to contain the image. During testing I thought things weren't working when I really had the TextField too small to display the whole image.
Flash Example
Simple example, all code is on the main timeline.
Create a dynamic TextField on the stage. Give it a useful name, I'm calling mine
txtImageTest
.Resize
txtImageTest
to a decent size, e.g. 300x150px.Create a new MovieClip symbol and give it a class name, e.g.
imageClip1
.Draw something in your new clip or place an embedded image inside
imageClip1
.Go back to the main timeline, deselect all objects and open the Actionscript editor on the first frame.
Turn on multiline and word-wrapping on the text field:
Save and test your movie.
Flex Example
Since we can't just create a new MovieClip in the library like we did in Flash we need to create a new class that performs the same task (this method also works in Flash if you want want to create a new clip in the library).
Here is my class for a black triangle bullet, called BlackArrow.as:
NOTE: Do not extend Bitmap (in Flash) or BitmapAsset (in Flex) as they do not scale or position properly in the TextField. Choose Sprite or something similar.
Here is an example of a class that displays this image in the TextField:
Note that I am using the fully-qualified class name in the
src
attribute of the image tag.Final Notes
Images are aligned to the left or right of a text field as determined by the
align
attribute of the image tag. This means that you cannot position an image in the middle of text without accessing the image directly.A list of the attributes supported by the image tag is here:
http://blog.coursevector.com/notes-htmltext
The TextField LiveDoc page is here:
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/text/TextField.html
The
getImageReference()
function is what you need to get a reference the images in the textfield:http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/text/TextField.html#getImageReference%28%29
Make sure you use fully-qualified class names in the
src
attribute.您可以将嵌入图像指定为 htmlText 中 HTML img 标记的 src,方法是使用完全限定类名(您可以使用
getFullyQualifiedClassName
确定该类名)来引用它们。 就像这样:这比为可能嵌入的每个图像创建包装类要简单得多......
You can specify embedded images as the src for HTML img tags in htmlText by referencing them using their fully qualified class name, which you can determine using
getFullyQualifiedClassName
). Like so:That's much simpler than creating a wrapper class for every image which might be embedded...
尝试:
请参阅文档。
Try:
See documentation.
我一直在寻找同样的东西。 使用嵌入图像背后的想法是防止从外部 URL 加载所述图像。 所以接受的答案实际上并没有解决问题。 我看过文档,他们也在那里使用 URL,并且该图像不会被编译到 swf 中,但会在运行时从服务器加载。 为什么他们称该嵌入图像可能是因为它嵌入在文本中,但我正在寻找的是使用嵌入在编译代码中的图像。
为什么要在 htmlText 中使用嵌入图像显示? 因为这是在工具提示中显示图像的最简单方法(您只需重写默认工具提示类以设置 htmlText 而不是文本,并将其设置为 TooltipManager 中的默认工具提示类)。 在我的搜索中,我发现了这个:http://groups .google.com/group/flex_india/browse_thread/thread/cf0aa62afaae3fdc/b21f462f8d5da117?pli=1 。 还没有测试过,我猜问题在于确定链接 id。 当我得到更多细节时,我会回来提供更多细节。
I was looking for the same thing. The idea behind using embedded images is to prevent loading of said images from an external URL. So the accepted answer actually isn't solving the problem. I've seen the documentation, and they too use an URL there, and that image will not be compiled into the swf, but will be loaded at runtime from the server. Why they call that embedded image is probably because it's embedded withing the text, but what I'm looking for is to use an image that is embedded in the compiled code.
Why would you want an embedded image show used in a htmlText? Because it's the easiest way to display an image in a tooltip (you just need to override to default tooltip class to set the htmlText instead of text, and set it as the default tooltip class in the TooltipManager). In my search I discovered this: http://groups.google.com/group/flex_india/browse_thread/thread/cf0aa62afaae3fdc/b21f462f8d5da117?pli=1 . Haven't test it yet, and I guess the problem will come in determining that linkage id. I'll come back with more details when I have them.
Sly_cardinal 写了一个非常有用的答案。 非常感谢他!
这个问题我折腾了半天也没能解决。 Sly_cardinal 注意到,如果这样做,getQualifiedClassName(嵌入位图)图像将不会正确地插入到文本中的左上角。
Sly_cardinal 提出制作一个包装类。
我想附加一个他的答案
一个新的类包装器
ImageWrapper.as->
代码中的调用:
就是这样。
谢谢你!
Sly_cardinal wrote a very helpful answer. Big thanks to him!
I could not solve this problem for half a day. Sly_cardinal noticed that if you do so getQualifiedClassName (embed bitmap) image is inserted in the text is not correctly just to the upper left corner.
Sly_cardinal offered to make a wrapper class.
I want to append an his answer
A new class wrapper
ImageWrapper.as ->
The call in the code:
That's it.
Thank you!
尝试
取自:使用 htmlText 属性
Try
Taken from: Using the htmlText property
使用 src='assets/img.gif',但您必须在部署应用程序 SWF 的服务器目录中创建一个 asset 目录,并将 img.gif 文件放在那里。 当在 html 中时,它会查找与 SWF 文件相关的文件。
这将允许您在开发和部署位置使用相同的文件和位置。
Use src='assets/img.gif' but you will have to create an assets directory in the directory of the server where the application SWF is deployed and place your img.gif file there. When in html, it is looking for the file relative to the SWF file.
This will allow you to use the same file and location in both development and deployed locations.
它可以在本地主机上运行,因为您的计算机上有图像,因此您可以加载它。 如果要嵌入它,它不会上传到服务器上的指定目录中,而是包含在 .SWF 文件本身中。 将图像上传到生产输出中的相应位置(浏览图像以从源文件文件夹上传)。
It works on localhost because you have the image on the machine, so you can load it. If you are embedding it, it is not uploaded on the server in the specified directory, rather it is contained by the .SWF file itself. Upload the image (browse it for upload from you source files folder) in the corresponding place in the production output.
不用再麻烦自己了!从现在起,导入MovieClips不再是一件大事了! 借助名为 TextArea 的新类(不是 TextArea 组件,它是一个 OOP 类,是原始 TextField 类的扩展)。
我们不再需要嵌入或库,我们可以在任何地方使用 TextArea 而不是 Textfield,这使我们能够导入我们自己的 SWF 文件,例如视频播放器、会说话的头像或其他任何内容我们的文本行。
它还有更多功能,并且不具体限于此问题。
查看 www.doitflash.com 了解更多信息,该网站提供该平台,下载也是免费的收费:)
There's no need to bother ourselves any more buddies! from now on importing MovieClips is not such a big deal! by the help of the new class named TextArea (not TextArea the component, it's an OOP class an extenstion to the original TextField class).
We don't need Embedding or library any more, we can use the TextArea every where instead of Textfield and that enable us to import our own SWF files such as a video player, talking avatar or anything else in line of our texts.
It has lots of more features and doesn't specifically limited to this issue.
Check out www.doitflash.com for more information, the site provides the platform and downloading it is also free of charge :)