简体   繁体   English

TRichEdit 中同一行中的彩色文本

[英]Colorful text in the same line in TRichEdit

How to write text in the same line but with different color?如何在同一行中写入不同颜色的文本? (I use richedit). (我使用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;

Best Wishes, Bee最好的祝福,蜜蜂

You're on the right track.你在正确的轨道上。 Just change SelAttributes and use SelText instead of Lines.Add :只需更改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;

This produces这产生

上面代码的示例输出

if you are using themes... answer above won't work..如果您正在使用主题......上面的答案将不起作用..
you cannot see any colors... till you remove seFont from styles..你看不到任何颜色......直到你从样式中删除 seFont ......

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

eg.例如。

....
amsg:='Hola';

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

For the last piece of text in the line, include a carriage return to finish off the line.对于行中的最后一段文本,包括回车以结束该行。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM