如何在 TextEdit 中更改数字的字体颜色

How to change font color of numbers in TextEdit

我想知道是否可以在 TextEdit 中更改某些数字的字体颜色。

例如,蓝色全为“1”,红色全为“2”等...

如果这在 TextEdit 中是不可能的,有人可以推荐一款能够这样做并读取 .txt 文件的类似软件吗?

这很有趣! macOS 包括 Perltextutil 用于更改文档格式,所以你可以做你想做的事情而不需要安装任何...

#!/bin/bash
perl -pe 'BEGIN{ $/=; }
          s/0/<font color="red"    >0<font color="black">/ || 
          s/1/<font color="green"  >1<font color="black">/ ||
          s/2/<font color="blue"   >2<font color="black">/ ||
          s/3/<font color="cyan"   >3<font color="black">/ ||
          s/4/<font color="magenta">4<font color="black">/ ||
          s/5/<font color="yellow" >5<font color="black">/ ||
          s/6/<font color="pink"   >6<font color="black">/ ||
          s/7/<font color="orange" >7<font color="black">/ ||
          s/8/<font color="teal"   >8<font color="black">/ ||
          s/9/<font color="gray"   >9<font color="black">/ ||
          s/\n/<br>/;
         ' ""  | textutil -format html -convert rtf -stdin -stdout > output.rtf

将其另存为 go 并使其可执行,只需一次:

chmod +x go

然后您可以像这样转换任何文档:

./go input.txt

这将使 output.rtf.

因此,如果我在 input.txt 中从以下内容开始:

1234567890 I love RTF and HTML but not 2 much and I can't spell, or count 1,000,222
1 man had 2 dogs and 333 cats

我最终得到:


Perl 一次读取 1 个字符,因为 $/=;。然后它用不同颜色的 HTML font 标签替换任何数字,然后在数字之后将颜色设置回黑色。然后将输出传递给 textutil,后者被告知将输入视为 html 并生成 RTF 输出。


我在这上面花了一整天的时间,但我仍然不知道为什么您的一个文档有效而另一个无效。我唯一可以说的是,当我注释掉以下将文档中 1 的颜色更改为绿色的行时,textutil 似乎不会崩溃!我不知道为什么!

s/1/<font color="green"  >1<font color="black">/ ||

我什至用不同的方式重写了它,但它仍然是一样的。我将代码放在下面,以便其他人可以玩它:

#!/usr/bin/env perl
use strict;
use warnings;

   my ($i, $x, %HofH);

   # Initialise table mapping input bytes to output/actions
   # So it's a hash of hashes, each hash stores the corresponding output string and whether it set colour
   foreach $i ('A'..'Z', 'a'..'z' ){
      $x = ord($i);
      $HofH{$x}{out}    = $i;
   }
   # Set up the coloured digits 0-9
   $i=48; $HofH{$i}{out} = sprintf("<font color='red'>0")    ; $HofH{$i}{colour}=1;
   #$i=49; $HofH{$i}{out} = sprintf("1")                      ; $HofH{$i}{colour}=1;    # THIS CRASHES TEXTUTIL ???
   $i=50; $HofH{$i}{out} = sprintf("<font color='blue'>2")   ; $HofH{$i}{colour}=1;
   $i=51; $HofH{$i}{out} = sprintf("<font color='cyan'>3")   ; $HofH{$i}{colour}=1;
   $i=52; $HofH{$i}{out} = sprintf("<font color='magenta'>4"); $HofH{$i}{colour}=1;
   $i=53; $HofH{$i}{out} = sprintf("<font color='yellow'>5") ; $HofH{$i}{colour}=1;
   $i=54; $HofH{$i}{out} = sprintf("<font color='pink'>6")   ; $HofH{$i}{colour}=1;
   $i=55; $HofH{$i}{out} = sprintf("<font color='orange'>7") ; $HofH{$i}{colour}=1;
   $i=56; $HofH{$i}{out} = sprintf("<font color='teal'>8")   ; $HofH{$i}{colour}=1;
   $i=57; $HofH{$i}{out} = sprintf("<font color='grey'>9")   ; $HofH{$i}{colour}=1;

   # Special cases - CR, LF, ampersand
   $i=9;  $HofH{$i}{out} = " ";
   $i=10; $HofH{$i}{out} = "<br>\n";
   $i=13; $HofH{$i}{out} = "<br>\n";
   $i=39; $HofH{$i}{out} = "&amp;";
   $i=47; $HofH{$i}{out} = "/";
   $i=61; $HofH{$i}{out} = "=";
   $i=63; $HofH{$i}{out} = "?";
   $i=40; $HofH{$i}{out} = "(";
   $i=41; $HofH{$i}{out} = ")";
   $i=45; $HofH{$i}{out} = "-";
   $i=58; $HofH{$i}{out} = ":";
   $i=59; $HofH{$i}{out} = ";";
   $i=46; $HofH{$i}{out} = ".";
   $i=44; $HofH{$i}{out} = ",";
   $i=32; $HofH{$i}{out} = " ";
   $i=60; $HofH{$i}{out} = "&lt;";
   $i=62; $HofH{$i}{out} = "&gt;";

   open(my $FILE, $ARGV[0]) or die $!;
   binmode($FILE);

   local $/ = ;
   my $cnt=0;
   while ( my $byte = <$FILE> ) {
      my $x = ord($byte);
      if(defined $HofH{$x}){
         printf("%s",$HofH{$x}{out});
         printf("<font color='black'>") if defined $HofH{$x}{colour}
      } else {
         printf STDERR "ERROR: Character number %d ignored at byte offset %d\n",$x,$cnt;
      }
      $cnt++;
   }

   close $FILE;

你运行它与:

thisScript.pl someFile.txt | textutil -format html -convert rtf -stdin -stdout > output.rtf