如何在 livecode 中给单词上色

How to color a word in livecode

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

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

但我无法更改 "organization" 的颜色。这怎么可能? 是否有任何选项可以在单个代码行中替换和着色 "organization"?

您不能只告诉 LiveCode 更改 "organization"(或 "oranges" 的颜色)。一个快速的方法是:

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

如果单词 "organisation" 有多个实例,这将特别有用。请注意,此代码替换了单词并同时为其添加了颜色。

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

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。

试试这个处理器

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