delphi TRichEdit中同一行的彩色文本

xlpyo6sf  于 2023-04-20  发布在  其他
关注(0)|答案(3)|浏览(158)

如何在同一行中用不同的颜色写文本?(我使用richedit)。

procedure TForm1.btnEClick(sender: TObject);
begin

  m0.SelAttributes.Color := clBlue;
  m0.SelAttributes.Style := [fsBold];
  m0.lines.add('This is blue and it is bold');
  m0.SelAttributes.Color := clGreen;
  m0.SelAttributes.Style := [fsBold];
  m0.lines.add ('This is Green and it is bold');
  m0.lines.add('');
  m0.lines.add('But how to write text in the same line with different color?');
  // i want to have both blue and green in the same line 
end;

最好的祝愿,蜜蜂

a1o7rhls

a1o7rhls1#

你的思路是正确的。只需更改SelAttributes并使用SelText而不是Lines.Add

procedure TForm4.FormCreate(Sender: TObject);
begin
  RichEdit1.Clear;
  RichEdit1.SelAttributes.Color := clBlue;
  RichEdit1.SelAttributes.Style := [fsBold];
  RichEdit1.SelText := 'This is bold blue text.';
  RichEdit1.SelAttributes.Color := clRed;
  RichEdit1.SelAttributes.Style := [fsItalic];
  RichEdit1.SelText := #32'This is italic red text';
end;

这产生

m4pnthwp

m4pnthwp2#

如果你正在使用主题...上面的答案将不起作用..
你看不到任何颜色...直到你从样式中删除seFont ..

RichEdit1.styleElements:=richedit1.styleElements-[seFont];

例如

....
amsg:='Hola';

RichEdit1.SelStart := RichEdit1.GetTextLen();
RichEdit1.SelAttributes.Color := acolor;
RichEdit1.Lines.Add(amsg + sLineBreak);
RichEdit1.SelLength := Length(amsg + sLineBreak);
1wnzp6jl

1wnzp6jl3#

对于行中的最后一段文本,包括回车以结束该行。

RichEdit1.SelAttributes.Color := clGreen;
RichEdit1.SelAttributes.Style := [];
RichEdit1.SelText := 'This is the last piece of text on the line.' + #13;

相关问题