【问题标题】:AS3: Getting the x & y value of a matched string in a text fieldAS3:获取文本字段中匹配字符串的 x 和 y 值
【发布时间】:2011-10-04 04:51:13
【问题描述】:

我在 Flash 中有一个文本字段,其中包含(显然)文本块。

我想要做的是对返回找到文本的x & y 坐标和width & height 的文本字段的内容执行搜索。结果将用于在文本框的该部分上放置一个可视元素。

例如:

var pattern:RegExp = /\d+/g;
var found:Array = box.text.match(pattern);

var i:String;
for each(i in found)
{
    /**
     * Find the x, y, width, height of the matched text in the text field
     * Use result to place transparent yellow box around text
     */
}

这在视觉上应该会产生类似的结果:

【问题讨论】:

  • 有趣的问题,我一直想知道这是怎么做到的!

标签: regex flash actionscript-3 textfield


【解决方案1】:

您可能希望使用 TextField 类的 getCharBoundaries 方法,该方法接受字符索引并返回一个 Rectangle 对象。

据我所知,您的 match 方法会返回字符串本身,所以首先您可能需要使用 String 类的 indexOf 方法,找到所有字符索引,将它们传递给 getCharBoundaries 方法,然后根据该输出绘制一些矩形。

Here's some reference.

祝你好运!

【讨论】:

  • 啊,很好。我正在使用 match() 来解析正则表达式(在这种情况下查找数字)并且匹配的将使用 indexOf()。谢谢。
  • 谢谢。我以为我必须做自动换行数学之类的。很高兴这存在。
猜你喜欢
  • 1970-01-01
  • 2015-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多