如何在livecode中为单词着色

时间:2015-03-26 04:58:55

标签: livecode

我想将"organisation"替换为"organization",并在Livecode中将组织的颜色更改为红色。我使用此代码进行替换:

--replace "organisation" with "organization" in field "MytextField"---

但我无法改变"组织"的颜色。这怎么可能? 是否可以选择更换和着色"组织"在一个单一的代码行?

2 个答案:

答案 0 :(得分:2)

试试这个处理程序

on replace_and_highlight pFieldName, pTarget, pReplacement, pColor
   set the caseSensitive to true
   put the length of pTarget      into tTargetLength
   put the length of pReplacement into tReplacementLength
   repeat
      put offset(pTarget, field pFieldName) into tStart
      if tStart is 0 then EXIT repeat
      put pReplacement into     char tStart to \
            tStart + tTargetLength      - 1 of field pFieldName
      set the foregroundColor of char tStart to \
            tStart + tReplacementLength - 1 of field pFieldName to pColor
   end repeat
end replace_and_highlight

实施例

on mouseUp
   -- replace plurals first, invert to see why
   replace_and_highlight "Data", "Organisations", "Organizations", red 
   replace_and_highlight "Data", "organisations", "organizations", red
   replace_and_highlight "Data", "Organisation" , "Organization" , red
   replace_and_highlight "Data", "organisation" , "organization" , red

end mouseUp

答案 1 :(得分:1)

你不能告诉LiveCode改变"组织的颜色" (或"橙子"就此而言)。快速执行此操作的方法是:

put the htmlText of field "myTextField"" into myHtml
set the caseSensitive to true
replace "organisation" with "<font color=" & quote & "red" & quote & ">organization</font>" in myHtml
replace "Organisation" with "<font color=" & quote & "red" & quote & ">Organization</font>" in myHtml
set the htmlText of fld "myTextField" to myHtml

如果您有多个单词&#34; organization&#34;的实例,这将特别有用。请注意,此代码会替换该单词并同时为其添加颜色。

如果您只想替换该单词的一个实例,您还可以执行以下操作:

set the caseSensitive to true
repeat for each item myWord in "organisation,Organisation"
  put wordoffset(myWord ,field "myTextField") into myOffset
  put "z" into char 7 of word myOffset of field "myTextField"
  set the textColor of word myOffset of fld "myTextField" to red
end repeat

此脚本查找单词的位置并将s更改为z并将颜色应用于该单词。订单很重要。如果你反过来这样做,你会在一个红字中得到一个黑色的z。