Colorful text in the same line in TRichEdit

早过忘川 提交于 2019-12-05 20:03:19

问题


How to write text in the same line but with different color? (I use 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


回答1:


You're on the right track. Just change SelAttributes and use SelText instead of 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




回答2:


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;


来源:https://stackoverflow.com/questions/20185264/colorful-text-in-the-same-line-in-trichedit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!